【问题标题】:Examine iOS Push Notification Payload检查 iOS 推送通知负载
【发布时间】:2014-11-04 20:11:58
【问题描述】:

我试图弄清楚如何检查推送通知的有效负载,以确定当用户从通知中打开应用程序时打开哪个视图。例如,如果通知显示“x:test”,则在点击通知时会打开视图 x,如果通知显示“y:test”,则会打开视图。

编辑:我想我应该澄清我不确定的部分。

我在 didFinishLaunchingWithOptions 中有这个:

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (notification)
    {
        // check payload then load appropriate view controller
    }

如何检查特定文本的有效负载以确定要加载的适当视图控制器?

【问题讨论】:

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


【解决方案1】:

这是我过去项目的代码。通知显示在类似“Kostas:想加你为朋友”的设备中。

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

NSDictionary *remoteNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (remoteNotif) {

    NSString *name = [[NSString alloc] init];
    NSString *message = [[NSString alloc] init];
    // 'wants to add you as friend.'
    NSString* alertValue = [[remoteNotif valueForKey:@"aps"] valueForKey:@"alert"];

    NSMutableArray* parts = [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@": "]];
    name = [parts objectAtIndex:0];
    [parts removeObjectAtIndex:0];
    message = [parts componentsJoinedByString:@": "];

    if ([message isEqualToString:@": wants to add you as friend."]) {
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        // tabController.delegate = self;
        tabController.selectedIndex = 1;
    }
    else{
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        // tabController.delegate = self;
        tabController.selectedIndex = 2;

        [self addMessageFromRemoteNotification:remoteNotif updateUI:NO];
    }

【讨论】:

    【解决方案2】:

    在应用委托中的两个地方处理接收推送通知

    • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 当您的应用程序从推送通知启动时。在这种情况下,您的推送通知数据包含在[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]

    • - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo当你的应用运行时收到通知。在这种情况下,userInfo 是您的推送通知数据。

    【讨论】:

    • 查看我的更新。我已经澄清了我不确定的地方。
    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2020-02-20
    • 2017-04-19
    相关资源
    最近更新 更多