0

我對Objective-C語言完全陌生。我需要實現的是現在我的應用上的推送通知位。我使用javapns庫在XCode 6和Java的服務器端編寫了客戶端。現在,在服務器管理髮送通知(我收到確認消息)的同時,我的設備上沒有收到任何內容,無論該應用是處於活動狀態還是在後臺運行。已發送但未收到iOS推送通知

有人請指導我在正確的方向嗎?謝謝!

#import "AppDelegate.h" 

@interface AppDelegate() 

@end 

@implementation AppDelegate 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; 
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

return YES; 
} 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken{ 
//Store the Device Token 
NSLog(@"%@", newDeviceToken); 
} 

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ 
NSLog(@"Failed to register with error: %@", error); 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
NSLog(@"Push received: %@", userInfo); 
} 

服務器端:

public class PushServer { 
public static void main(String[] args) { 
    try { 
     BasicConfigurator.configure(); 
     Push.alert("Message!", "***.p12", "***", false, 
       "92ab*************91af4"); 
    } catch (CommunicationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (KeystoreException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

} 

這是我收到的輸出,當我嘗試發送通知:

0 [main] DEBUG javapns.notification.Payload - Adding alert [Message!] 
210 [main] DEBUG javapns.communication.ConnectionToAppleServer - Creating SSLSocketFactory 
229 [main] DEBUG javapns.communication.ConnectionToAppleServer - Creating SSLSocket to gateway.sandbox.push.apple.com:2195 
1077 [main] DEBUG javapns.notification.PushNotificationManager - Initialized Connection to Host: [gateway.sandbox.push.apple.com] Port: [2195]: 735b478[SSL_NULL_WITH_NULL_NULL: Socket[addr=gateway.sandbox.push.apple.com/17.172.232.46,port=2195,localport=53762]] 
1079 [main] DEBUG javapns.notification.PushNotificationManager - Building Raw message from deviceToken and payload 
1080 [main] DEBUG javapns.notification.PushNotificationManager - Built raw message ID 1 of total length 73 
1080 [main] DEBUG javapns.notification.PushNotificationManager - Attempting to send notification: {"aps":{"alert":"Message!"}} 
1080 [main] DEBUG javapns.notification.PushNotificationManager - to device: 92a**********1af4 
2327 [main] DEBUG javapns.notification.PushNotificationManager - Flushing 
2327 [main] DEBUG javapns.notification.PushNotificationManager - At this point, the entire 73-bytes message has been streamed out successfully through the SSL connection 
2327 [main] DEBUG javapns.notification.PushNotificationManager - Notification sent on first attempt 
2327 [main] DEBUG javapns.notification.PushNotificationManager - Reading responses 
2749 [main] DEBUG javapns.notification.PushNotificationManager - Found 0 notifications that must be re-sent 
2749 [main] DEBUG javapns.notification.PushNotificationManager - No notifications remaining to be resent 
2749 [main] DEBUG javapns.notification.PushNotificationManager - Closing connection 

任何幫助表示讚賞。

+0

您是否在供應配置文件中啓用了推送通知? https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/ConfiguringPushNotifications/ConfiguringPushNotifications.html – dstudeba

+0

是的,我有。仍然沒有運氣。有沒有可能會丟失的某種處理程序? – Somya

+0

我發現推送通知可能令人沮喪地實現和調試。如果您再次檢查了是否包含允許通知的適當配置文件,則應嘗試其他設備和/或第三方通知API,例如Parse,Intercom或Localytics。另請注意,您的代碼適用於iOS 8. – dstudeba

回答

1

偶然的情況下,你是否正在用Xcode 7構建並定位iOS 9?

如果是的話,你很可能是存在其他需要安全訪問的URL,如這裏所描述的新的安全默認設置:

Disabling ATS for an in-app browser in iOS 9?

而且,你似乎是混合的老方法與新的。

registerUserNotificationSettings是替換registerForRemoteNotificationTypes的iOS 8方法。

所以,只有使用前者,它看起來像只針對iOS 8及更高版本。

但是,然後您使用didReceiveRemoteNotification:,這是該方法的舊版本,對應於registerForRemoteNotificationTypes。但是,由於您使用的是較新版本,因此您應該使用 didReceiveRemoteNotification:fetchCompletionHandler。

+0

我在XCode 6.4上構建了一個iOS 8.3設備。 我試過使用didReceiveRemoteNotification:fetchCompletionHandler – Somya

-1

您需要將NSData轉換爲字符串並通過在您的方法中實施以下更改來獲取實際設備令牌。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSString *myToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] 
           stringByReplacingOccurrencesOfString: @">" withString: @""] 
          stringByReplacingOccurrencesOfString: @" " withString: @""]; 
    // *** Now user `myToken` to send notification *** 
    NSLog(@"%@",myToken); 
} 

隨着它,你需要確保你正在運行與被用於在您的服務器端發送推送通知相同的臨時配置文件(開發者/分發)應用程序。兩者應該是一樣的。我希望它對你很清楚。