2015-10-29 37 views
0

我是初學者寫關於IOS快速的推送。它適用於應用程序在前臺運行,但無法在後臺應用程序或關閉時運行。 順便說一句,我可以通過APNS.newPayload()。alert在應用程序處於後臺或關閉狀態時收到警報消息。 非常感謝您的幫助。ios push messsage無法在後臺接收

以下是我的服務器和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

回答

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方法後,應用程序在設備註冊成功完成時調用此方法。在實現此方法時,請連接您的推送通知服務器並將令牌給予它。 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

+0

謝謝你。 我已經在AppDelegate中註冊了推送通知。 我可以在前臺收到消息。我的問題是我無法在後臺收到。 – beehuang

+0

當你使用上面的代碼時,你可以在後臺接收消息還是關閉? – beehuang

+0

我更新了上面的答案。 – Thomas

相關問題