0

我是初学者用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 推送在前台而不是后台工作

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

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

}

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

参考 APN后台刷新,设置AppDelegate.m

4

1 回答 1

0

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

您必须在 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 的推送通知设置。您可以在以下帖子中找到如何执行此操作:重置应用程序的推送通知设置

于 2015-10-29T14:45:05.003 回答