Scheduling local notification from within a Today extension

£可爱£侵袭症+ 提交于 2019-11-28 21:35:43

If anyone comes by this question, this is now achievable in iOS 10 and it's new UserNotifications framework. To schedule local notification from your app extension you can just call this line of code from within your extension:

UNUserNotificationCenter.current().add(<#UNNotificationRequest#>)

I think this may become possible if you do this:

  1. Whenever you want to schedule a local notification, make your today extension create the notification and write it's information to a file in a shared container.

  2. (You need a server) Contact your server form your today extension, ask your server to send a slient push notification (content-available: 1), which will then wake your main app up.

  3. When your app is woken up by the remote notification, reads the file created from step 1 from the shared container, schedules the local notification then goes back to sleep.

This is extremely unlikely to work. App extensions aren't allowed to access sharedApplication. The fact that it's possible to prevent the compiler from noticing that you're doing so doesn't change that. If you try this in a debugger, I'd guess that sharedApplication is actually returning nil and that this is why the code is failing.

If you can find some way to make it work, don't rely on it. Apple would almost certainly regard that as a bug and fix it in a future release, breaking your extension without warning.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!