当应用程序一天不活动时,GCM 推送通知不会出现在 iPhone 上

Posted

技术标签:

【中文标题】当应用程序一天不活动时,GCM 推送通知不会出现在 iPhone 上【英文标题】:GCM Push Notifications are not coming on iPhone when app is not active for one day 【发布时间】:2017-06-07 08:28:00 【问题描述】:

在 iPhone/iPad 上安装应用后,应用可以第一时间收到通知。一旦应用程序处于非活动状态,它就会停止在前台和后台收到通知。

有人可以指出,我错过了什么。自从我第一次收到以来,消息格式似乎没有问题。

服务器代码:

            message.put("priority", "high");
            message.put("content_available",true);
            if (to != null)
            
                message.put("to", to.replace("\\", ""));
            
            if (messageId != null)
            
                message.put("message_id", messageId);
            
            JSONObject subobj = new JSONObject();
            subobj.put("sound", "default");
            message.put("notification", subobj);

            message.put("data", payload);
            if (timeToLive != null)
            
                message.put("time_to_live", timeToLive);
            
            if (delayWhileIdle != null && delayWhileIdle)
            
                message.put("delay_while_idle", true);
            
            if (collapseKey != null)
            
                message.put("collapse_key", collapseKey);
            
            message.put("delivery_receipt_requested", true);

客户代码:

     - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

// First run Delete keychain

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"]) 
    // Delete values from keychain here

    application.applicationIconBadgeNumber = 0;

    [self resetKeychain];
    [[NSUserDefaults standardUserDefaults] setValue:@"1strun" forKey:@"FirstRun"];
    [[NSUserDefaults standardUserDefaults] synchronize];


self.viewController = [[MainViewController alloc] init];


CGRect screenBounds = [[UIScreen mainScreen] bounds];

#if __has_feature(objc_arc)
    self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
   self.window = [[[UIWindow alloc] initWithFrame:screenBounds] 
autorelease];
#endif
self.window.autoresizesSubviews = YES;
//notification

[self updateDurationLabel];
UIFont *font = [UIFont boldSystemFontOfSize:10.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[self.segFromStyle setTitleTextAttributes:attributes forState:UIControlStateNormal];
[self.segToStyle setTitleTextAttributes:attributes forState:UIControlStateNormal];

[self flowManager];


UILocalNotification *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) 
    application.applicationIconBadgeNumber = 0;
    self.launchNotification = remoteNotification.userInfo;

    NSLog(@"NotificationCheck: remoteNotification");



UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if (localNotification) 
    application.applicationIconBadgeNumber = 0;
    NSLog(@"NotificationCheck: localNotification");

    self.launchNotification = localNotification.userInfo;

    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:localNotification.userInfo options:0 error:nil];
    NSString* jsonString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];

    NSLog(@"Dict:%@", jsonString);
      
[self.window makeKeyAndVisible];

return YES;

【问题讨论】:

【参考方案1】:

您可以参考此thread,其中建议将"priority": "high" 添加到JSON 以在后台获取通知。


  "to" : "token...",
  "priority": "high",
  "notification" : 
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  

其他参考资料:

GCM push notification to ios with content_available (not working to invoke from inactive state) GCM support for ios application when application in background or killed Making GCM work for iOS device in the background

【讨论】:

感谢您的回复。我确实已经设置了 priority 和 content_available 标志。 [message.put("优先级", "高"); message.put("content_available",true);] 代码中唯一缺少的标志是'badge',让我试试运气。

以上是关于当应用程序一天不活动时,GCM 推送通知不会出现在 iPhone 上的主要内容,如果未能解决你的问题,请参考以下文章

设备处于睡眠模式时不会出现 Android GCM 通知

当应用程序处于非活动状态时,GCM 推送通知不适用于 iOS

应用程序完全关闭时不出现 Android 推送通知(使用 node-gcm)

从 GCM 推送通知打开地图活动

关闭应用程序时如何避免处理来自 GCM 的推送通知

当应用程序未运行时,GCM 推送通知未在某些设备中显示