在 iOS 上不使用 GCM 接收推送通知
Posted
技术标签:
【中文标题】在 iOS 上不使用 GCM 接收推送通知【英文标题】:Don't receive push notifications with GCM on iOS 【发布时间】:2015-08-09 17:31:09 【问题描述】:我按照本教程 https://developers.google.com/cloud-messaging/ios/client 在我的 iOS 应用程序上实现 Google Cloud Messaging。我使用谷歌应用引擎作为服务器端和 GCM 的 Java 库来发送消息,它非常适合我的 android 应用程序。但是我的 iOS 应用程序没有收到来自它的消息。
我不明白为什么,因为我确实获得了注册令牌并将其发送到我的服务器端。我没有收到任何错误日志。这是我的 AppDelegate.m 代码:
#import "AppDelegate.h"
@interface AppDelegate ()
@property (nonatomic, strong) NSDictionary *registrationOptions;
@property (nonatomic, strong) GGLInstanceIDTokenHandler registrationHandler;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//-- Start the GCMService
[[GCMService sharedInstance] startWithConfig:[GCMConfig defaultConfig]];
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
else
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
self.registrationHandler = ^(NSString *registrationToken, NSError *error)
if (registrationToken != nil)
//I do get the token, and I can store it
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:registrationToken forKey:CLE_TOKENGCM];
NSLog(@"Registration Token: %@", registrationToken);
else
NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
;
return YES;
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
// Start the GGLInstanceID shared instance with the default config and request a registration
// token to enable reception of notifications
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken");
[[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];
self.registrationOptions = @kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:@NO;//I tried both YES and NO value
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:SENDER_ID
scope:kGGLInstanceIDScopeGCM
options:self.registrationOptions
handler:self.registrationHandler];
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
NSLog(@"Error in registration. Error: %@", err);
- (void)onTokenRefresh
// A rotation of the registration tokens is happening, so the app needs to request a new token.
NSLog(@"The GCM registration token needs to be changed.");
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:SENDER_ID
scope:kGGLInstanceIDScopeGCM
options:self.registrationOptions
handler:self.registrationHandler];
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
NSLog(@"Notification received: %@", userInfo);//This log never prints so the method is never called
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
NSLog(@"Notification received: %@", userInfo);//This log never prints so the method is never called
// This works only if the app started the GCM service
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
@end
【问题讨论】:
您是否生成了 APN 证书并在服务器上使用它们? 您是否尝试过sample GCM iOS project,它会在日志中为您提供更多信息,例如,如果您错过了 APN 证书。 我在 didFinishLaunchingWithOptions 中将 registrationToken 设为 nil。有什么想法吗? 【参考方案1】:感谢 ztan 评论和 Google GCM sample,我发现了问题所在:我必须像这样实现 applicationDidBecomeActive:
:
- (void)applicationDidBecomeActive:(UIApplication *)application
// Connect to the GCM server to receive non-APNS notifications
[[GCMService sharedInstance] connectWithHandler:^(NSError *error)
if (error)
NSLog(@"Could not connect to GCM: %@", error.localizedDescription);
else
NSLog(@"Connected to GCM");
// ...
];
【讨论】:
以上是关于在 iOS 上不使用 GCM 接收推送通知的主要内容,如果未能解决你的问题,请参考以下文章