iOS 本地推送、远程推送及带快速回复的本地推送

注意:
1. 快速回复功能仅 iOS8.0及以后版本支持
2. cancelAllLocalNotifications 方法会取消所有本地推送,包括使用 fireData 设定的未来之间点的本地推送

本地推送

属性如下图所示。
这里写图片描述

需要注意的是,如果要使用系统默认声音,soundName 需要设置为 UILocalNotificationDefaultSoundName

category 属性也是 8.0 之后的新 API ,这里的 category 和 UIUserNotificationCategory 中的 identifier 一致。如果对应的 category 设置了快速回复按钮,则使用这个 category 的 notification 就有对应的功能

快速回复

1.创建消息上要添加的动作,以按钮的形式显示

//接受按钮
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"acceptAction";
acceptAction.title = @"接受";
acceptAction.activationMode = UIUserNotificationActivationModeForeground;
//拒绝按钮    
UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];
rejectAction.identifier = @"rejectAction";
rejectAction.title = @"拒绝";
rejectAction.activationMode = UIUserNotificationActivationModeBackground;
rejectAction.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
rejectAction.destructive = YES;

2.创建动作(按钮)的类别集合

UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";
NSArray *actions = @[acceptAction, rejectAction];
[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal];

3.创建UIUserNotificationSettings,并设置消息的显示类型

// 这里需要注意,如果之前已经注册一个 settings 这里注册完成后会覆盖之前的
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];

4.注册推送

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

在使用Push的时候需要在数据包中加入特定的Category字段(字段内容需要前后端定义为一致),终端接收到到后,就能展示上述代码对应Category设置的按钮,和响应按钮事件。

 {"aps":{"alert":"测试推送的快捷回复", "sound":"default", "badge": 1, "category":"alert"}}

说明:Push数据包之前能带的数据最多为256字节,现在APPLE将该数值放大到2KB。

按钮处理事件

以本地通知为例介绍怎么处理,如下代码是发起本地通知事件:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @"测试推送的快捷回复";
notification.category = @"alert";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

2.实现下面的Delegate方法

(1)成功注册registerUserNotificationSettings后回调的方法

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"%@", notificationSettings);
}

(2)事件处理

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    //在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
    NSLog(@"%@----%@",identifier,notification);
    //处理完消息,最后一定要调用这个代码块
    completionHandler();
}

常用 payload 字段

KeyValue typeComment
alertstring or dictionaryIf this property is included, the system displays a standard alert or a banner, based on the user’s setting. You can specify a string or a dictionary as the value of alert. If you specify a string, it becomes the message text of an alert with two buttons: Close and View. If the user taps View, the app launches. If you specify a dictionary, refer to Table 5-2 for descriptions of the keys of this dictionary. The JSON \U notation is not supported. Put the actual UTF-8 character in the alert text instead.
badgenumberThe number to display as the badge of the app icon. If this property is absent, the badge is not changed. To remove the badge, set the value of this property to 0.
soundstringThe name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container. The sound in this file is played as an alert. If the sound file doesn’t exist or default is specified as the value, the default alert sound is played. The audio must be in one of the audio data formats that are compatible with system sounds; see Preparing Custom Alert Sounds for details.
content-availablenumberProvide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed, application:didReceiveRemoteNotification:fetchCompletionHandler: is called.
categorystringProvide this key with a string value that represents the identifier property of the UIMutableUserNotificationCategory object you created to define custom actions. To learn more about using custom actions, see Registering Your Actionable Notification Types.

Table 5-2 Child properties of the alert property

KeyValue typeComment
titlestringA short string describing the purpose of the notification. Apple Watch displays this string as part of the notification interface. This string is displayed only briefly and should be crafted so that it can be understood quickly. This key was added in iOS 8.2.
bodystringThe text of the alert message.
title-loc-keystring or nullThe key to a title string in the Localizable.strings file for the current localization. The key string can be formatted with %@ and %n$@ specifiers to take the variables specified in the title-loc-args array. See Localized Formatted Strings for more information. This key was added in iOS 8.2.
title-loc-argsarray of strings or nullVariable string values to appear in place of the format specifiers in title-loc-key. See Localized Formatted Strings for more information. This key was added in iOS 8.2.
action-loc-keystring or nullIf a string is specified, the system displays an alert that includes the Close and View buttons. The string is used as a key to get a localized string in the current localization to use for the right button’s title instead of “View”. See Localized Formatted Strings for more information.
loc-keystringA key to an alert-message string in a Localizable.strings file for the current localization (which is set by the user’s language preference). The key string can be formatted with %@ and %n$@ specifiers to take the variables specified in the loc-args array. See Localized Formatted Strings for more information.
loc-argsarray of stringsVariable string values to appear in place of the format specifiers in loc-key. See Localized Formatted Strings for more information.
launch-imagestringThe filename of an image file in the app bundle, with or without the filename extension. The image is used as the launch image when users tap the action button or move the action slider. If this property is not specified, the system either uses the previous snapshot,uses the image identified by the UILaunchImageFile key in the app’s Info.plist file, or falls back to Default.png.This property was added in iOS 4.0

推送的详细信息及 payload 支持的字段请参照官方文档《Local and Remote Notification Programming Guide》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值