【问题标题】:Didreceivenotification never gets called although i receive the notification尽管我收到了通知,但从未调用过 Didreceivenotification
【发布时间】:2014-06-21 15:13:06
【问题描述】:
(void)application:(UIApplication *)application
     didReceiveRemoteNotification:(NSDictionary *)userInfo ;

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:
                    (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult 
                     result))completionHandler{};

从来没有被调用,虽然我在 iphone 上收到通知,是否需要添加任何其他功能才能使其正常工作?谢谢。

更新 当应用程序不在后台也不处于活动状态但我没有收到消息时,我尝试了这个。 - (BOOL)应用程序:(UIApplication *)应用程序 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 为 |GPPSignIn| 设置应用程序的客户端 ID和 |GPPShare|。

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

// Clear application badge when app launches
application.applicationIconBadgeNumber = 0;
if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) {

    id userInfo=[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"MESSAGE" message:userInfo delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
}

【问题讨论】:

  • 此方法在应用程序处于活动状态或背景时调用,如果您的应用程序关闭则此方法永远不会调用。
  • 那么,如果应用程序不在后台或处于活动状态,我可以实现任何功能来接收推送通知吗?
  • 是的,在 didFinishLaunchingWithOptions..
  • 请确定,您的应用程序是在后台还是前台?如果它在前台,我确定调用了该方法。如果不是,则可能您没有将该方法放入您的应用程序委托中。

标签: ios push-notification


【解决方案1】:

如果您的应用程序关闭并收到通知.....

didFinishLaunchingWithOptions中添加以下代码

if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) 
{    
    id userInfo=[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    NSLog(@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"MESSAGE" message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
}

但是当你点击通知时这个电话

【讨论】:

  • 我试过但没用,请检查我更新的问题
  • 您是否尝试过终止状态...表示应用程序已关闭
  • 谢谢 Nitin,是的,它起作用了,但现在的问题是,当我点击推送通知时,它会打开屏幕,不到一秒钟它就会再次关闭它,不知道为什么!
  • UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"MESSAGE" message:userInfo delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; [警报显示];删除这个
  • 我不知道如何在 NSLOG 中打印它,好像我从后台删除了应用程序,它将不再附加到 Xcode :( 这就是我使用 alertview 的原因,有什么想法吗?
猜你喜欢
  • 2020-11-19
  • 2021-11-27
  • 1970-01-01
  • 2018-03-16
  • 2018-10-09
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
相关资源
最近更新 更多