ios - 在 iOS 上不使用 GCM 接收推送通知
<p><p>我按照本教程 <a href="https://developers.google.com/cloud-messaging/ios/client" rel="noreferrer noopener nofollow">https://developers.google.com/cloud-messaging/ios/client</a>在我的 iOS 应用程序上实现 Google Cloud Messaging。我使用谷歌应用引擎作为服务器端和 GCM 的 Java 库来发送消息,它非常适合我的 Android 应用程序。但是我的 iOS 应用程序没有收到来自它的消息。</p>
<p>我不明白为什么,因为我确实获得了注册 token 并将其发送到我的服务器端。我没有收到任何错误日志。这是我的 AppDelegate.m 代码:</p>
<pre><code>#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
[ startWithConfig:];
if ()
{
// iOS 8 Notifications
];
;
}
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 = ;
;
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");
[ startWithConfig:];
self.registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:@NO};//I tried both YES and NO value
[ 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.");
[ 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
[ 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
[ appDidReceiveMessage:userInfo];
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// ...
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>感谢 ztan 评论和 Google <a href="https://developers.google.com/cloud-messaging/ios/start" rel="noreferrer noopener nofollow">GCM sample</a> ,我发现出了什么问题:我必须像这样实现 <code>applicationDidBecomeActive:</code>:</p>
<pre><code>- (void)applicationDidBecomeActive:(UIApplication *)application {
// Connect to the GCM server to receive non-APNS notifications
[ connectWithHandler:^(NSError *error) {
if (error) {
NSLog(@"Could not connect to GCM: %@", error.localizedDescription);
} else {
NSLog(@"Connected to GCM");
// ...
}
}];
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 iOS 上不使用 GCM 接收推送通知,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31906927/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31906927/
</a>
</p>
页:
[1]