AWSSNS(Amazone推送通知),应用程序崩溃

时间:2017-02-13 08:02:24

标签: objective-c push-notification amazon-sns

您好我想实施amazone提供的snspush通知服务。 我为此安装了pod 在podfile中我写   pod'AWSSNS' 之后 pod install

然后我实现这样的代码

当我运行app时,会发生以下日志崩溃

****由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因是:'服务配置为nil。在使用此方法之前,您需要配置Info.plist或设置defaultServiceConfiguration。'*

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIMutableUserNotificationCategory *messageCategory = [[UIMutableUserNotificationCategory alloc] init];
messageCategory.identifier = @"ShareAction";
UIMutableUserNotificationAction *notificationAction = [[UIMutableUserNotificationAction alloc] init];
notificationAction.identifier = @"ShareAction";
notificationAction.title = @"Share";
notificationAction.activationMode = UIUserNotificationActivationModeForeground;
notificationAction.authenticationRequired = YES;
notificationAction.destructive = NO;
[messageCategory setActions:@[notificationAction] forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:messageCategory];

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

[[UIApplication sharedApplication] registerForRemoteNotifications];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
NSString *deviceTokenString = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"deviceTokenString: %@", deviceTokenString);
[[NSUserDefaults standardUserDefaults] setObject:deviceTokenString forKey:@"deviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];

AWSSNS *sns = [AWSSNS defaultSNS];
AWSSNSCreatePlatformEndpointInput *request = [AWSSNSCreatePlatformEndpointInput new];
request.token = deviceTokenString;
request.platformApplicationArn = SNSPlatformApplicationArn;
[[sns createPlatformEndpoint:request] continueWithBlock:^id(AWSTask *task) {
    if (task.error != nil) {
        NSLog(@"Error: %@",task.error);
    } else {
        AWSSNSCreateEndpointResponse *createEndPointResponse = task.result;
        NSLog(@"endpointArn: %@",createEndPointResponse);
        [[NSUserDefaults standardUserDefaults] setObject:createEndPointResponse.endpointArn forKey:@"endpointArn"];
        [[NSUserDefaults standardUserDefaults] synchronize];



    }

    return nil;
}];

}

如果有人知道,请提供帮助。

1 个答案:

答案 0 :(得分:0)

您必须使用此代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}