【问题标题】:Remote Notifications in "Not running" State处于“未运行”状态的远程通知
【发布时间】:2014-02-10 13:38:00
【问题描述】:

我在我的应用程序中实现了远程通知!如果我的应用程序处于后台并且推送消息已发送到我的设备,我会使用此方法做出反应:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}

当应用程序处于前台或后台状态时,这非常有用!但是,如果我的应用程序根本没有运行怎么办?!当应用程序未运行时,我不能对推送消息做出反应吗?我的意思是 WhatsApp 可以做到这一点,对吧?!

【问题讨论】:

标签: ios iphone objective-c ios7 push-notification


【解决方案1】:

如果用户点击通知中心的推送通知,您将在 launchOptions 中获得带有推送通知内容的信息,您可以使用下面的代码检查应用程序是否已启动并点击推送通知,或者它也在那里,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];        
    NSLog(@"LaunchOptions->%@",launchOptions);
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [self performNotificationAction:userInfo];
    }

    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//    NSLog(@"userInfo->%@",userInfo);
    [self performNotificationAction:userInfo];
}

-(void)performNotificationAction:(NSDictionary*)userInfo{
    //Do the stuf whatever you want.
    //i.e. fetch the message or whatever extra information sent in push notification
}

【讨论】:

  • 我不希望用户点击任何东西...我只想发送一条静默推送消息并在后台处理它!但是,如果我的应用程序没有运行/终止,则不会调用给定的方法……但是 WhatsApp 是怎么做到的呢?!
  • 在 iOS7 中他们添加了一个新方法可以调用您的应用程序,但部署目标仅限于 iOS7。
  • 是的,我知道!但它不起作用!正如您在我的问题中看到的,我已经实现了 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler!但是如果应用程序终止,则不会调用此方法
  • 应该像他们在文档中所说的那样调用它Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多