菜鸟教程小白 发表于 2022-12-12 15:56:38

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 &#34;AppDelegate.h&#34;

@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 &lt; 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(@&#34;Registration Token: %@&#34;, registrationToken);
    } else {
      NSLog(@&#34;Registration to GCM failed with error: %@&#34;, 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(@&#34;didRegisterForRemoteNotificationsWithDeviceToken&#34;);
[ 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(@&#34;Error in registration. Error: %@&#34;, err);
}

- (void)onTokenRefresh {
// A rotation of the registration tokens is happening, so the app needs to request a new token.
NSLog(@&#34;The GCM registration token needs to be changed.&#34;);
[ tokenWithAuthorizedEntity:SENDER_ID
                                                    scope:kGGLInstanceIDScopeGCM
                                                options:self.registrationOptions
                                                handler:self.registrationHandler];
}


- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(@&#34;Notification received: %@&#34;, 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(@&#34;Notification received: %@&#34;, 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(@&#34;Could not connect to GCM: %@&#34;, error.localizedDescription);
      } else {
            NSLog(@&#34;Connected to GCM&#34;);
            // ...
      }
    }];
}
</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]
查看完整版本: ios - 在 iOS 上不使用 GCM 接收推送通知