您的位置:首页 > 移动开发 > IOS开发

iOS 推送笔记

2015-11-02 09:04 495 查看
    iOS 8,iOS  9 推送做了一些改变,以前的推送内容限制512B,现在扩充到2k吧。而且iOS以后推送注册方式也发生了改变! 主要是下面红色部分!我们要先注册用户通知。
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
action1.title = @"删除";
action1.identifier  = @"删除test";
action1.activationMode = UIUserNotificationActivationModeBackground;

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
action2.title = @"回复";
action2.identifier = @"回复test";
[action2 setBehavior:UIUserNotificationActionBehaviorTextInput];
action2.activationMode = UIUserNotificationActivationModeBackground;

UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";//这组动作的唯一标示
[categorys setActions:@[action1,action2] forContext:UIUserNotificationActionContextMinimal];

<span style="color:#ff0000;"> UIUserNotificationType type = UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
UIUserNotificationTypeSound;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObject:categorys]];
[application registerForRemoteNotifications];
[application registerUserNotificationSettings:settings];</span>

}

注册本地通知:

UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];//本地推送间隔多久时间发生
notification.fireDate = fireDate;
notification.timeZone = [NSTimeZone defaultTimeZone];//当前时区
notification.repeatInterval = kCFCalendarUnitDay;//重复间隔
notification.alertBody = @"该起床了...";//通知栏显示的文字
notification.applicationIconBadgeNumber = 1;//APP数字
notification.soundName = UILocalNotificationDefaultSoundName;//声音
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];
notification.userInfo = userDict;//参数字典
//按上面约定时间执行通知
//[[UIApplication sharedApplication] presentLocalNotificationNow:notification];//立刻执行
[[UIApplication sharedApplication] scheduleLocalNotification:notification];//按约定时间执行


推送的扩张
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"action";//按钮的标示
action.title=@"回复";//按钮的标题
[action setBehavior:UIUserNotificationActionBehaviorTextInput];//点击这个按钮会弹出输入框
action.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候启动程序
//action.authenticationRequired = YES;//是否需要解锁
action.destructive = YES;//

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按钮
action2.identifier = @"action2";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
action.destructive = YES;

UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";//这组动作的唯一标示
[categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];

UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:[NSSet setWithObjects:categorys, nil]];

这里当你的本地通知也加入扩展时 

    notification.category =
@"alert"; 的标题必须与 categorys.identifier = @"alert";//这组动作的唯一标示 

扩展的操作
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{

if ([identifier isEqualToString:@"回复test"]) {

NSInteger num = [responseInfo[UIUserNotificationActionResponseTypedTextKey] integerValue];

application.applicationIconBadgeNumber += num;

NSLog(@"回复");

}
else if([identifier isEqualToString:@"删除test"])
{

application.applicationIconBadgeNumber = 90;

NSLog(@"删除");
}

completionHandler();
}


你可以在这里面操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: