iOS交互推送

iOS 8提供的3个新类:UIUserNotificationSettings,UIUserNotificationCategory,UIUserNotificationAction以及它们的变体。

和 以前简单地注册通知类型(sounds、banners、alerts)相比,现在你可以注册自定义的通知类别(categories)和动作 (actions)。类别描述了应用自定义的通知类型,并且包含用户能够执行的响应动作。比如,你收到一个通知说某人在社交网上了关注了你,作为回应你可 能会想要关注他或者忽略。

#define NotificationCategoryIdent  @"ACTIONABLE"

#define NotificationActionOneIdent  @"ACTION_ONE"

#define NotificationActionTwoIdent  @"ACTION_TWO"

注册通知

UIMutableUserNotificationAction*action1;

action1=[[UIMutableUserNotificationActionalloc]init];

[action1setActivationMode:UIUserNotificationActivationModeBackground];

[action1setTitle:@"Action1"];

[action1setIdentifier:NotificationActionOneIdent];

[action1setDestructive:NO];

[action1setAuthenticationRequired:NO];

UIMutableUserNotificationAction*action2;

action2=[[UIMutableUserNotificationActionalloc]init];

[action2setActivationMode:UIUserNotificationActivationModeBackground];

[action2setTitle:@"Action2"];

[action2setIdentifier:NotificationActionTwoIdent];

[action2setDestructive:NO];

[action2setAuthenticationRequired:NO];

UIMutableUserNotificationCategory*actionCategory;

actionCategory=[[UIMutableUserNotificationCategoryalloc]init];

[actionCategorysetIdentifier:NotificationCategoryIdent];

[actionCategorysetActions:@[action1,action2]

forContext:UIUserNotificationActionContextDefault];

NSSet*categories=[NSSetsetWithObject:actionCategory];

UIUserNotificationTypetypes=(UIUserNotificationTypeAlert|

UIUserNotificationTypeSound|

UIUserNotificationTypeBadge);

UIUserNotificationSettings*settings;

settings=[UIUserNotificationSettingssettingsForTypes:types

categories:categories];

[[UIApplicationsharedApplication]registerUserNotificationSettings:settings];

}

要发送这个通知类型,只需简单的将category添加到声明里。

{"aps":{"alert":"测试","category":"ACTIONABLE","badge":1,"sound":"default","custom":{"t":"jump","p":"second"}}}

现在为了响应用户选择的操作,你需要在UIApplicationDelegate协议添加两个新方法:

application:handleActionWithIdentifier:forLocalNotification:completionHandler:

application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

用户从你的推送通知中选择一个动作后,该方法将会在后台被调用。

-(void)application:(UIApplication*)applicationhandleActionWithIdentifier:(NSString*)identifierforRemoteNotification:(NSDictionary*)userInfocompletionHandler:(void(^)())completionHandler{

if([identifierisEqualToString:NotificationActionOneIdent]){

NSLog(@"Youchoseaction1.");

}

elseif([identifierisEqualToString:NotificationActionTwoIdent]){

NSLog(@"Youchoseaction2.");

}

if(completionHandler){

completionHandler();

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 许多集成的步骤个推官网都有了,这里只写关于推送的远程推送和本地通知的步骤和代码。APP在后台时:走苹果的APNS通...
    AllureJM阅读 7,830评论 1 9
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,720评论 10 16
  • 一 本地推送 使用 UIApplication 注册一个通知 //快捷回复 UIMutableUserNotif...
    筱x阅读 5,093评论 0 0
  • 1.NSTimer //暂停if ([timer isValid]) {[timer setFireDate:[N...
    俊月阅读 5,202评论 0 0
  • 2017-2-18 周六 上午6点抵达新加坡樟宜机场,机场很大,免税店多。特别注意的是,如果转机乘客坐的不是联程航...
    夜木犀阅读 3,339评论 1 3

友情链接更多精彩内容