如果应用程序在前台运行时忽略远程通知,但在单击通知栏上的通知以启动应用程序时对其进行响应,该怎么办?
答案 0 :(得分:2)
当应用程序位于前台时,通知栏中不会显示通知。通知有效负载将传递给application:didReceiveRemoteNotification:
方法,如果这是你想要的,你可以忽略它。
当通知到达时应用程序在后台运行时,当您打开应用程序时,也会调用application:didReceiveRemoteNotification:
。您可以使用以下代码区分这两种情况:
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if([app applicationState] == UIApplicationStateInactive)
{
//application was running in the background
}
}
当您通过点击通知打开应用时,通知有效内容会传递到另一个名为application:didFinishLaunchingWithOptions:
的方法,您可以在其中处理它。
答案 1 :(得分:0)
我更喜欢这个组合。如果没有我在didFinishLaunchingWithOptions
中添加的功能,当应用首次从通知点按启动到内存时,通知将不会通过didReceiveRemoteNotification
中包含的逻辑进行深层链接。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// All your nice startup code
// ...
// Hook for notifications
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
}
这两个都在你的AppDelegate中,顺便说一句。
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive) {
return;
}
// Do anything you want with the notification, such as deep linking
// ...
}