第一次没有收到推送通知

Push notification is not receiving very first time

我正在从节点服务器向 iPhone 和 Android 发送推送通知。它已成功发送到 APNS/GCM 并成功接收到移动设备的通知。但是,在 iPhone 6 中,推送通知在收到通知后并不是第一次收到。它发生在以下阶段:

  1. 正在安装应用程序并首次打开。
  2. 如果应用程序不再使用。

提前致谢..

  1. 有时可能不会收到推送通知。
  2. 代码是为所有场合写的? (应用关闭/活动/非活动/后台)
  3. 该代码针对相同的场合进行了测试?

A​​pple 在 Local and Push Notification Programming Guide and Troubleshooting Push Notifications

中说

Delivery of notifications is a “best effort”, not guaranteed. It is not intended to deliver data to your app, only to notify the user that there is new data available.

更新:

问题是 Apple 在无法处理消息时会关闭连接。因此,如下更新节点推送通知 server.js 中的代码,它运行良好:

var connectCallback = function (err) {
  // gracefully handle auth problems
  if (err && err.name === 'GatewayAuthorizationError') {
    console.log('Authentication Error: %s', err.message);
    process.exit(1);
  }

  // handle any other err (not likely)
  else if (err) {
    throw err;
  }

  // it worked!
  var env = agent.enabled('sandbox')
    ? 'sandbox'
    : 'production';

  console.log('apnagent [%s] gateway connected', env);
};

agent.connect(connectCallback);

setInterval(function(){
  agent.close(function(param1){
    agent.connect(connectCallback);
  });
}, 20 * 60 * 1000);

我遇到了同样的问题,我通过以下方式解决了它:-

在 didFinishLaunchingWithOptions 方法中写这个

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        } else {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];
        }

它将向用户触发权限警报。 如果他允许,则在成功方法中发送设备令牌:-

#pragma PushNotification delegation

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    NSString *device = [deviceToken description];
    device = [device stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    device = [device stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"My deviceToken is: %@", device);

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:device forKey:@"deviceToken"];

     // Now send device token to server
}