didReceiveRemoteNotification将用户带到正确的视图 [英] didReceiveRemoteNotification take user to the correct view

查看:91
本文介绍了didReceiveRemoteNotification将用户带到正确的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个聊天应用程序,我的服务器在发送新消息时发送推送通知。我遇到的问题是如何让用户看到正确的视图?我在推送通知中发送 channelID ,但我如何检索它并将用户带到实际的对话?

I've got a chat application where my server sends out push notifications when a new message is send. The problem I'm having is how can i take the user to the correct view? Im sending a channelID in the push notification but how can i retrieve it and take the user to the actual conversation?

我正在使用此代码来检测点击推送通知的时间

I'm using this code to detect when a push notification was clicked

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}


推荐答案

如果您在推送通知中发送 channelID ,则可以检索 channelID来自userInfo字典的
正如midhere所说 -


1) 当应用程序在后台运行时当应用程序在前台运行时
应用程序:didReceiveRemoteNotification:方法将调用如下。

If you are sending channelID in push notification than you can retrieve channelID from userInfo dictionary. As midhere said -

1) When application is running in background and When application is running in foreground application:didReceiveRemoteNotification: method will called as below.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
     {
     //opened from a push notification when the app was on background

       NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
       NSLog(@"channelID->%@",channelID);
     }
  else if(application.applicationState == UIApplicationStateActive)
     {
     // a push notification when the app is running. So that you can display an alert and push in any view

       NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
       NSLog(@"channelID->%@",channelID);
     }
}

2) 当应用程序未启动(关闭)而不是应用程序时:将调用didFinishedLaunchWithOptions 方法。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if (launchOptions != nil)
    {
         //opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
            NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
            NSLog(@"channelID->%@",channelID);
        }

    }
     else{
             //opened app without a push notification.
         }
}

这篇关于didReceiveRemoteNotification将用户带到正确的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆