iphone - didRegisterForRemoteNotificationsWithDeviceToken : doesn't invoke on calling registerForRemoteNotificationTypes:?

标签 iphone push-notification ios-4.2 apple-push-notifications

我正在 navBased 应用程序中处理推送通知。在 AppDelegate.m didRegisterForRemoteNotificationsWithDeviceToken: 调用 registerForRemoteNotificationTypes 时不会调用:代码如下所示:

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

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

    return YES;
}



- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Get a hex string from the device token with no spaces or < >
    NSLog(@"applicationDidFinishLaunchingWithOptions dev token test");

    NSString *deviceTokenStr = [[[[deviceToken description]
                          stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                         stringByReplacingOccurrencesOfString: @">" withString: @""] 
                        stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"Device Token: %@", deviceTokenStr);
}

我很确定配置文件不是问题。我发现错误:

注册时出错。错误:错误域=NSCocoaErrorDomain代码=3000“找不到应用程序的有效'aps-environment'权利字符串”UserInfo=0x115490 {NSLocalizedDescription=没有找到应用程序的有效'aps-environment'权利字符串}

谁能告诉我这段代码发生了什么并且它不起作用?谢谢

最佳答案

在 Appdelegate didFinishLaunching 方法中输入此代码 -

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

然后将这两个方法复制并粘贴到同一个 appdelegate 中 -

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
        NSString *dToken = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    dToken = [dToken stringByReplacingOccurrencesOfString:@" " withString:@""];


    NSLog(@"STR%@",dToken);


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{

    NSLog(@"Failed to get token, error: %@", error);
}

关于iphone - didRegisterForRemoteNotificationsWithDeviceToken : doesn't invoke on calling registerForRemoteNotificationTypes:?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4994302/

相关文章:

ios - Ionic 3 + One Signal handleNotificationReceived 不会被触发

php - 使用 PHP 的 Azure 通知中心 REST API

ios - 如何在 UIDatePickerModeTime 中设置时间范围

iphone - iOS plot 3d图形库

iphone - 调用具有多个 subview 层次结构的协议(protocol)方法

react-native - 实现从 salesforce Marketing Cloud 到 React Native 移动应用程序的推送通知

objective-c - 在设备上测试应用程序什么都不做

css - 让 CSS 对 iPhone 上的电子邮件中的触摸使用react

iphone - 如何在 iPhone 的 monotouch 中创建 SQLite 数据库?

macos - 安装 Xcode 3.2.5 和 iOS SDK 4.2 时出错