如何在 ios 中正确设置推送通知 [英] How to properly set Push Notifications in ios

查看:80
本文介绍了如何在 ios 中正确设置推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I am stuck with this issue for over a week now. I followed this tutorial here to add Push Notification in my app. On initial run of my app the "App would like to send you push notification" is not appearing. But when I go to the Apps Notification it is already registered for notification for sounds and banners only and the Badge App Icon is not ON. But when I Login in my app the Badge App Icon Notification Type is no longer there. (sorry cant post image)

当我检查我的日志时,我遇到了这个运行时错误:试图标记应用程序图标,但没有收到用户标记应用程序的许可

And when I check my logs I have this run time error: Attempting to badge the application icon but haven't received permission from the user to badge the application

这是我在 AppDelegate.m 中的代码

Here's my code in my 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];
}

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

Any help will be appreciated. Thanks.

推荐答案

我刚刚尝试使用以下方式 &它起作用了..

I just tried using below way & it worked..

#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 添加以下方法

Add below method for 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

这篇关于如何在 ios 中正确设置推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆