【问题标题】:How to get all received notifications on ios [duplicate]如何在ios上获取所有收到的通知[重复]
【发布时间】:2013-03-07 15:33:28
【问题描述】:

如果推送通知已发送到设备,是否可以知道 iOS 应用程序何时启动? (我想访问有效负载以从通知中检索信息)

谢谢,

【问题讨论】:

    标签: ios notifications push-notification apple-push-notifications


    【解决方案1】:

    你应该在你的代码中添加这样的东西:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
    *)launchOptions {
    
        NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    
        //Accept push notification when app is not open
        if (remoteNotif) {      
            [self handleRemoteNotification:application userInfo:remoteNotif];
            return YES;
        }
    
        return YES;
    }
    

    请注意,只有通过点击通知启动应用程序时,您才会收到推送通知负载。如果它是通过点击应用图标启动的,您将无法获得有效负载。

    【讨论】:

    • 要挑剔,建议的处理程序方法应该有签名 -handleRemoteNotification:remoteNotif application:application
    【解决方案2】:

    有来自UIApplicationDelegate的方法,你可以从中查看是否收到通知

    您可以在 AppDelegate didFinishLaunchingWithOptions 方法中看到用户是否启动了带有通知的应用程序,例如

    UILocalNotification *notif =
        [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (localNotif) {
            irLog(@"Recieved Notification");
        }
    

    对于您发布的本地通知,您可以查看此方法

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
    

    对于远程通知,你可以看看这个方法

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    

    【讨论】:

      猜你喜欢
      • 2023-02-04
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      相关资源
      最近更新 更多