简体   繁体   English

iOS:处理远程(推送)通知

[英]iOS: Handling Remote (push) Notifications

I'm trying to handle all possible cases with remote notifications. 我正在尝试通过远程通知处理所有可能的情况。 I'm ok when app in foreground - didReceiveRemoteNotification is called. 我在前台应用程序没问题-didReceiveRemoteNotification被调用。 The problem is when app in background state, and I receive push notification. 问题是当应用程序处于后台状态时,我收到推送通知。 Nothing is called. 什么都没叫。 how to let user know that he has new remote notification when app come back to foreground ? 当应用回到前台时,如何让用户知道他有新的远程通知?

The only way for you to intercept a push notification is when the user tap the notify in the notification center (or when slide the app icon from the lock screen). 拦截推送通知的唯一方法是用户在通知中心点击通知(或从锁定屏幕滑动应用程序图标)。

In this case before the app go in foreground the didFinishLaunchingWithOptions method in the app delegate is called. 在这种情况下,在应用程序进入前台之前,将调用应用程序委托中的didFinishLaunchingWithOptions方法。 You should use the NSDictionary launchOptions to determine if the app has been launched from notification center or by tapping the icon (the normal use) 您应该使用NSDictionary launchOptions来确定应用程序是从通知中心启动还是通过点击图标(正常使用)启动

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *pushDic = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    if (pushDic != nil) {
        NSLog(@"Notification");
    }
    else {

    }
}

Just have a look into the programming guide: 只需看一下编程指南即可:

If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate's application:didFinishLaunchingWithOptions: method (if implemented); 如果点击了操作按钮(在运行iOS的设备上),则系统将启动应用程序,并且该应用程序将调用其委托人的应用程序:didFinishLaunchingWithOptions:方法(如果已实现); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications). 它传入通知有效负载(用于远程通知)或本地通知对象(用于本地通知)。

Of course, if your app is in background, there will be nothing called... 当然,如果您的应用程序在后台运行,则不会有任何事情...

If your app is not launched (not even suspended in background), the 如果您的应用未启动(甚至没有在后台暂停),则

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey): 将包含通知有效负载(键UIApplicationLaunchOptionsRemoteNotificationKey):

NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM