如何在iOS设备上获取GCM通知

3

你好,我是一个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];

}

但是它显示异常,如:

未解决的标识符“_registrationHandler”“didFinishLaunchingWithOptions”方法中的“weakSelf”以及在didRegisterForRemoteNotificationsWithDeviceToken方法中出现异常,例如未解决的标识符“_registrationOptions”

请有人帮我。


_registrationHandler变量是在某个地方定义了吗?weakSelf同样如此。 - Fonix
我刚刚按照这个教程:https://developers.google.com/cloud-messaging/ios/client 进行了操作。 - user5026700
如果您知道更好的方法,请发布 - user5026700
1
首先,这是Objective-C,而不是Swift(我已更改标签)。其次,您没有在任何地方定义这些变量,这就是为什么会出现此错误的原因 - 而您不理解这一点表明您可能根本不了解Objective-C。我建议您完成一个简单的Objective-C教程,例如apple.com上的教程:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html - Epaga
1个回答

2

我已经重构了你的代码,现在应该可以正常工作。

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教程开始,因为您的编译器问题非常琐碎。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接