如何在 iOS 中注册 GCM [英] How to register for GCM in iOS

查看:35
本文介绍了如何在 iOS 中注册 GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让 GCM 推送通知正常工作.我的问题是我不知道如何从 GCM 获取注册 ID.我可以从 APN 获得令牌就好了.但我不太确定接下来要做什么.我尝试按照教程进行操作,但它并不适合我.我是初学者,所以请明确.

I can't seem to get GCM push notifications working. My problem is I don't know how to get a registration ID from GCM. I can get a token from APN just fine. But I'm not quite sure what to do next. I tried following the tutorial but its not really working for me. I'm a beginner so please be explicit.

我想问的是,从 APN 获取令牌后,我该怎么办?

What I'm asking is, after obtaining a token from APN, then what do I do?

提前致谢.https://developers.google.com/cloud-messaging/ios/client

推荐答案

Registration Token 从 didRegisterForRemoteNotificationsWithDeviceToken 给注册处理程序

The Registration Token is given to the registration handler from didRegisterForRemoteNotificationsWithDeviceToken

以下所有代码均取自 Google 的 GCM 示例.

All code below is taken from the GCM Sample from Google.

首先,在您的应用程序中声明一个处理程序:didFinishLaunchingWithOptions:

First, declare a handler in your application:didFinishLaunchingWithOptions:

_registrationHandler = ^(NSString *registrationToken, NSError *error){
    if (registrationToken != nil) {
        weakSelf.registrationToken = registrationToken;
        NSLog(@"Registration Token: %@", registrationToken);
        NSDictionary *userInfo = @{@"registrationToken":registrationToken};
        [[NSNotificationCenter defaultCenter] postNotificationName:weakSelf.registrationKey
                                                            object:nil
                                                          userInfo:userInfo];
    } else {
        NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
        NSDictionary *userInfo = @{@"error":error.localizedDescription};
        [[NSNotificationCenter defaultCenter] postNotificationName:weakSelf.registrationKey
                                                            object:nil
                                                          userInfo:userInfo];
    }
};

在应用注册回调中调用您的处理程序:

Call your handler in the application registration callback:

- (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
    [[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];

_registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
                         kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:_gcmSenderID
                                                    scope:kGGLInstanceIDScopeGCM
                                                  options:_registrationOptions
                                                  handler:_registrationHandler];
}

要使用的 GCM 令牌只是注册处理程序中的NSString *registrationToken".

The GCM token to use is simply the "NSString *registrationToken" in the registration handler.

这篇关于如何在 iOS 中注册 GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆