objective-c 远程通知 iOS 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24216632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:20:19  来源:igfitidea点击:

Remote Notification iOS 8

objective-cios8apple-push-notifications

提问by quang thang

How can I get the Device Token for remote notification in iOS 8? I used the method didRegisterForRemoteNotificationsWithDeviceTokenin AppDelegatein iOS < 8, and it returned the device token. But in iOS 8, it does not.

如何在 iOS 8 中获取远程通知的设备令牌?我所使用的方法didRegisterForRemoteNotificationsWithDeviceTokenAppDelegate在IOS <8,并且它返回令牌的设备。但在 iOS 8 中,它没有。

回答by Madao

Read the code in UIApplication.h.

阅读 UIApplication.h 中的代码。

You will know how to do that.

你会知道如何做到这一点。

First:

第一的:

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

add Code like this

添加这样的代码

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert 
      | UIUserNotificationTypeBadge 
      | UIUserNotificationTypeSound) categories:nil];
  [application registerUserNotificationSettings:settings];
#endif
} else {
  UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
  [application registerForRemoteNotificationTypes:myTypes];
}

if you not using both Xcode 5 and Xcode 6,try this code

如果您没有同时使用 Xcode 5 和 Xcode 6,请尝试此代码

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
      |UIRemoteNotificationTypeSound
      |UIRemoteNotificationTypeAlert) categories:nil];
  [application registerUserNotificationSettings:settings];
} else {
  UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
  [application registerForRemoteNotificationTypes:myTypes];
}

(Thanks for @zeiteisen @dmur 's remind)

(感谢@zeiteisen @dmur 的提醒)



Second:

第二:

Add this Function

添加此功能

#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

And your can get the deviceToken in

你可以得到 deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

if it still not work , use this function and NSLog the error

如果它仍然不起作用,请使用此功能并 NSLog错误

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

回答by zeiteisen

The way to register for iOS 8 and keep supporting older versions

注册 iOS 8 并继续支持旧版本的方法

UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
                                                                                         |UIUserNotificationTypeSound
                                                                                         |UIUserNotificationTypeAlert) categories:nil];
    [application registerUserNotificationSettings:settings];
} else {
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [application registerForRemoteNotificationTypes:myTypes];
}

and in the app delegate add

并在应用程序委托中添加

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

iOS8 can receive silent notificaions without asking for permission. Call - (void)registerForRemoteNotifications. After this application:didRegisterForRemoteNotificationsWithDeviceToken:will be called

iOS8 可以在不征得许可的情况下接收静默通知。打电话 - (void)registerForRemoteNotifications。在这之后 application:didRegisterForRemoteNotificationsWithDeviceToken:将被称为

Note: the callback with the token is only called if the application has successfully registered for user notifications with the function below or if Background App Refresh is enabled.

注意:只有当应用程序通过以下功能成功注册用户通知或启用了后台应用程序刷新时,才会调用带有令牌的回调。

Check the Settings for you app if any notification type is enabled. If not, you will not get a device token.

如果启用了任何通知类型,请检查应用程序的设置。否则,您将不会获得设备令牌。

You are now able to obtain silent notifications with

您现在可以通过以下方式获得静默通知

aps {
content-available: 1
}

in the notification payload

在通知负载中

But notifications that appear still needs permission. Call

但是出现的通知仍然需要许可。称呼

UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:notificationSettings];

This code should ask for permission.

此代码应请求许可。

You should now be ready to get push notifications

您现在应该准备好接收推送通知

回答by Kyle Clegg

In my case I had made the necessary updates to request push notification access for iOS 7 and iOS 8, however I had not implemented the new callback for when an iOS 8 user grants access. I needed to add this method to my app delegate.

就我而言,我已经进行了必要的更新来请求 iOS 7 和 iOS 8 的推送通知访问权限,但是我没有实现 iOS 8 用户授予访问权限时的新回调。我需要将此方法添加到我的应用程序委托中。

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

回答by Michael Kniskern

If you are using Xamarin.iOS to build your mobile application, you can use this snippet of code to request push notification registration

如果你使用 Xamarin.iOS 构建你的移动应用程序,你可以使用这段代码来请求推送通知注册

if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
    UIUserNotificationType userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
    UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(userNotificationTypes, null);
    UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
else
{
    UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
} 

Also you will need to override DidRegisterUserNotificationSettingsmethod to get the device token returned from the APNS server:

您还需要覆盖DidRegisterUserNotificationSettings方法以获取从 APNS 服务器返回的设备令牌:

public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
{
    application.RegisterForRemoteNotifications();
}

回答by Ilker Baltaci

The answer of Madao (https://stackoverflow.com/a/24488562/859742) is right but....

Madao ( https://stackoverflow.com/a/24488562/859742)的答案是正确的,但是......

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge

should be more "correct"

应该更“正确”

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];

Those flags have the same bit mask values and thats why both would work but UIUserNotificationSettingsrequires UIUserNotificationTypenot UIRemoteNotificationType.

这些标志有相同的位掩码值和这就是为什么既会工作,但UIUserNotificationSettings要求UIUserNotificationType没有UIRemoteNotificationType

Apart from that I would call

除此之外,我会打电话

[application registerUserNotificationSettings:settings];

In the AppDelegatemethod (depending on the granted rights),

AppDelegate方法中(取决于授予的权限),

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

回答by Hussain KMR Behestee

I thing the better way to keep backward compatibility we can go with this approach, it is working for my case hope work for you. Also pretty easy to understand.

我认为保持向后兼容性的更好方法是我们可以采用这种方法,它适用于我的案例,希望对您有用。也很容易理解。

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

回答by Devang Tandel

UIUserNotificationType types = UIUserNotificationTypeBadge |
    UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

    UIUserNotificationSettings *mySettings =
    [UIUserNotificationSettings settingsForTypes:types categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    [application registerForRemoteNotifications];