【问题标题】:How to properly set Push Notifications in ios如何在 ios 中正确设置推送通知
【发布时间】:2015-04-17 04:10:48
【问题描述】:

嗨,我被这个问题困扰了一个多星期。我按照本教程here 在我的应用程序中添加推送通知。在我的应用程序的初始运行中,“应用程序想向您发送推送通知”没有出现。但是,当我转到应用程序通知时,它已经注册了仅针对声音和横幅的通知,并且徽章应用程序图标未打开。 但是当我在我的应用程序中登录时,徽章应用程序图标通知类型不再存在。 (抱歉不能发图)

当我检查我的日志时,我遇到了这个运行时错误:尝试为应用程序图标添加徽章,但未获得用户对应用程序添加徽章的权限

这是我在 AppDelegate.m 中的代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
                                                                                        UIUserNotificationTypeBadge |
                                                                                        UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeSound];
}

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

    [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

任何帮助将不胜感激。 谢谢。

【问题讨论】:

标签: ios objective-c iphone notifications


【解决方案1】:

我刚刚尝试使用下面的方法,它奏效了..

#ifdef __IPHONE_8_0
  //Right, that is the point
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif

为 iOS 8.0 添加以下方法

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}
#endif

【讨论】:

  • 我仍然得到相同的结果..我认为我的问题是未显示要求通知权限的弹出窗口..我尝试提前 1 周设置日期然后关闭我的设备,但我仍然得到相同的结果。知道如何显示弹出窗口..?
  • 使用另一个配置了推送通知的临时配置文件,您肯定会收到通知警报......如果一切都会好起来......
  • 我创建了新的应用程序 ID,新的配置文件,我能够显示弹出窗口..但是当我登录我的应用程序时,同样的行为我仍然得到运行时错误:“尝试徽章应用程序图标,但尚未收到用户授予应用程序徽章的权限”,并且通知类型中缺少徽章应用程序图标通知类型..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
相关资源
最近更新 更多