【问题标题】:ios push messsage can not receive in backgroundios推送消息无法在后台接收
【发布时间】:2015-10-29 14:08:32
【问题描述】:

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

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

iOS 代码

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 的帮助,我通过设置 content value = 1 来解决这个问题。

参考APN background refresh, setting up AppDelegate.m

【问题讨论】:

    标签: ios background push-notification javapns


    【解决方案1】:

    我在 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 方法后,当设备注册成功时,应用会调用该方法。在您实施此方法时,连接您的推送通知服务器并将令牌提供给它。 APNs 仅将通知推送到令牌所代表的设备。

    - (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

    【讨论】:

    • 谢谢你。我已经在 AppDelegate 中注册了推送通知。我可以在前台接收消息。我的问题是我无法在后台接收。
    • 使用上述代码时,能否在后台接收消息或关闭?
    • 我已调用 didRegisterForRemoteNotificationsWithDeviceToken 并获取设备令牌。因此服务器可以在前台使用令牌发送到设备。
    • 感谢您的帮助,我通过设置内容值 = 1 来解决问题。stackoverflow.com/questions/30104529/…
    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多