ios - 如何在 ios 中获取 GCM 通知

标签 ios objective-c

大家好,我是 iOS 的初学者。在我的项目中,我想获得 GCM 通知,因为我已经编写了一些代码,但它显示异常,我的代码如下:

#import "AppDelegate.h"
#import <Google/CloudMessaging.h>
#import "GGLInstanceID.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIUserNotificationType allNotificationTypes =  (UIUserNotificationTypeSound |
                                                    UIUserNotificationTypeAlert |  UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =  [UIUserNotificationSettings
                                             settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    _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];
        }
    };

    return YES;
}


- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    [[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];

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

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {


    NSLog(@"Notification received: %@", userInfo);
    // This works only if the app started the GCM service
    [[GCMService sharedInstance] appDidReceiveMessage:userInfo];

}

但是它显示了类似

的异常

unresolved identifier "_registrationHandler" and "weakSelf" in didFinishLaunchingWithOptions method and it exception in didRegisterForRemoteNotificationsWithDeviceToken method like unresolved identifier "_registrationOptions".

请帮帮我

最佳答案

我在这里重构了您的代码,它现在应该可以工作了。

static NSString *const INSTANCE_ID_REGISTRATION_NOTIF_KEY = @"instance-id-token";
static NSString *const GCM_SENDER_ID = @"<your sender id>"; // @"123456"

@interface AppDelegate ()

    @property (nonatomic, strong, readwrite) GGLInstanceIDTokenHandler registrationHandler;
    @property (nonatomic, strong, readwrite) NSString *registrationToken;

@end

@implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        UIUserNotificationType allNotificationTypes =  (UIUserNotificationTypeSound |
                                                        UIUserNotificationTypeAlert |  UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =  [UIUserNotificationSettings
                                                 settingsForTypes:allNotificationTypes categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

        return YES;
    }


    - (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

        [[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];

        NSDictionary *registrationOptions = @{
            kGGLInstanceIDRegisterAPNSOption:deviceToken,
            kGGLInstanceIDAPNSServerTypeSandboxOption:@YES
        };

        __weak typeof(self) weakSelf = self;
        GGLInstanceIDTokenHandler handler = ^(NSString *registrationToken, NSError *error){
            typeof(weakSelf) strongSelf = weakSelf;

            NSDictionary *userInfo;
            if (registrationToken != nil) {
                strongSelf.registrationToken = registrationToken;
                NSLog(@"Registration Token: %@", registrationToken);
                userInfo = @{ @"registrationToken" : registrationToken };
            } else {
                NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
                userInfo = @{ @"error" : error.localizedDescription };
            }

            [[NSNotificationCenter defaultCenter] postNotificationName:INSTANCE_ID_REGISTRATION_NOTIF_KEY
                                                                object:nil
                                                              userInfo:userInfo];

        };

        [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:GCM_SENDER_ID
                                                            scope:kGGLInstanceIDScopeGCM
                                                          options:registrationOptions
                                                          handler:handler];
    }

    - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo {
        NSLog(@"Notification received: %@", userInfo);
        // This works only if the app started the GCM service
        [[GCMService sharedInstance] appDidReceiveMessage:userInfo];
    }

@end

PS:正如@Epaga 在上面评论的那样,你真的应该尝试从一个简单的 obj-c 教程开始,因为你的编译器问题非常微不足道。

关于ios - 如何在 ios 中获取 GCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31446767/

相关文章:

iOS EXC_BAD_ACCESS KERN_INVALID_ADDRESS

ios - 如何在ios中为多张照片添加转场效果?

ios - 我有 1 个表格 View 和三个标签

ios - 允许子类使用子类委托(delegate)

ios - 如何让 UILabel 在单词末尾显示 "..."而不是截断它?

ios - 在 UIImageView 上添加渐变

ios - iPhone 6 的 UINavigationBar 背景图像

ios - Segue - 索引 0 超出空数组的范围

ios - 如何在UIView中绘制贝塞尔曲线

iphone - 我怎样才能避免 AlAssetLibrary 中的位置服务?我可以在不使用定位服务的情况下使用 AlAssetLibrary 检索文件吗?