ios push messsage无法在后台接收

时间:2015-10-29 14:08:32

标签: ios background push-notification javapns

我是初学者用swift写关于IOS推送的。它适用于应用程序在前台运行但在应用程序在后台或关闭时无法正常工作。 顺便说一句,当应用程序处于后台或关闭时,我可以通过APNS.newPayload()。alertBody接收警报消息。 非常感谢你的帮助。

以下是我的服务器和ios代码。

iOScode

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    print("Recived: \(userInfo)")
    var receiveMsg = userInfo["key1"] as! String
    print("receiveMsg = \(receiveMsg)")

} 

服务器代码

    ApnsService service =
    APNS.newService()
    .withCert("ooxx.p12","")
    .withSandboxDestination()
    .build();   


    String payload = APNS.newPayload().customField("key1", "M;senderGCMID5;senderUserID5;userName5;userMessage5").build();
        String token = "token";
        service.push(token, payload);

我已经阅读了有关此主题的一些问题,但我无法解决这个问题。

我在iOS代码中尝试了其他功能,如下所示,但它不起作用。

APNS push working in foreground but not background

我不知道如何实施。你可以教我吗?非常感谢

   func application
 (application: UIApplication,          didReceiveRemoteNotification userInfo: [NSObject : AnyObject],    fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) ->   Void) {
    var receiveMsg = userInfo["key1"] as! String
   print("receiveMsg = \(receiveMsg)")

}

感谢pralthom的帮助,我通过设置内容值= 1来解决问题。

参考APN background refresh, setting up AppDelegate.m

1 个答案:

答案 0 :(得分:0)

我在Objective-C中实现了推送通知。我没有swift中的代码,但我猜它非常相似...

您必须在AppDelegate中注册推送通知。 在didFinishLaunchingWithOptions委托中添加以下代码:

// Register for Push Notitications, if running iOS 8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
            UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                            UIUserNotificationTypeBadge |
                                                            UIUserNotificationTypeSound);
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                                     categories:nil];
            [application registerUserNotificationSettings:settings];
            [application registerForRemoteNotifications];
        } else {
            // Register for Push Notifications before iOS 8
            [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                             UIRemoteNotificationTypeAlert |
                                                             UIRemoteNotificationTypeSound)];
        }

然后你还必须在AppDelegate

中添加以下代理
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DEBUG" message:[NSString stringWithFormat:@"Are you sure to use the right provisionning ? %@", error.localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
    }

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //Do whatever you want with the push received

它适用于后台以及应用程序被杀死时。如果应用程序被杀,操作系统的应用程序状态会有所不同。

还有一个要添加的委托:didRegisterForRemoteNotificationsWithDevicetoken。 在调用UIApplication对象的registerForRemoteNotifications方法后,应用程序在设备注册成功完成时调用此方法。在您的此方法的实现中,连接您的推送通知服务器并将令牌提供给它。 APN仅将通知推送到令牌所代表的设备。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *formattedToken = [[[deviceToken description]
                                 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
                                stringByReplacingOccurrencesOfString:@" "
                                withString:@""];
    [[NSUserDefaults standardUserDefaults] setObject:formattedToken forKey:@"DEVICE_IDENTIFIER"];
    [[NSUserDefaults standardUserDefaults] synchronize];
// Send the device identifier to your server

我认为您没有注册设备,这就是推送仅在应用程序处于前台时出现的原因。

测试推送通知时,应重置iPhone的推送通知设置。您可以在以下帖子中找到如何执行此操作:Reset push notification settings for app