美文网首页iOS 开发 iOS Developer ios零碎记录
iOS 非常简单的 极光推送 -- LZ

iOS 非常简单的 极光推送 -- LZ

作者: 谭老师初中历史 | 来源:发表于2016-04-22 14:52 被阅读335次

极光: https://www.jpush.cn

  1. 先申请 获取appKey;

2.程序入口中 添加代码(就是这么简单 , 你只需要复制粘贴):

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

//极光推送

if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

NSMutableSet *categories = [NSMutableSet set];

UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];

category.identifier = @"identifier";

UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];

action.identifier = @"test2";

action.title = @"test";

action.activationMode = UIUserNotificationActivationModeBackground;

action.authenticationRequired = YES;

//YES显示为红色,NO显示为蓝色

action.destructive = NO;

NSArray *actions = @[ action ];

[category setActions:actions forContext:UIUserNotificationActionContextMinimal];

[categories addObject:category];

[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)                      categories:categories];

}else{

[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)                      categories:nil];

}

//启动SDK

[JPUSHService setupWithOption:launchOptions appKey:JPUSH_APPKEY channel:JPUSH_CHANNEL apsForProduction:isProduction];

//如果 App 状态为未运行,此函数将被调用,如果launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用户点击apn 通知导致app被启动运行;如果不含有对应键值则表示 App 不是因点击apn而被启动,可能为直接点击icon被启动或其他。

//    NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];



[JPUSHService registrationID]   //注册    这里返回的是字符串   可以接收,有可能要上传服务器,具体看需求

}

3.继续添加:

#pragma mark 注册消息推送失败

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

{

NSLog(@"消息推送失败");

}

#pragma mark 处理收到的消息推送

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

//获取 APNS 标准信息内容

NSDictionary *aps = [userInfo valueForKey:@"aps"];

NSString *content = [aps valueForKey:@"alert"];//推送显示的内容

int badge = [[aps valueForKey:@"badge"] intValue];//badge数量

NSString *sound = [aps valueForKey:@"sound"];//播放的声音

//取得Extras字段内容(额外的附加信息)

NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"];//服务端中Extras字段,key是自己定义的

NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content, (long)badge,sound,customizeField1);

UILocalNotification *local = [JPUSHService setLocalNotification:[NSDate dateWithTimeIntervalSinceNow:1] alertBody:content badge:badge alertAction:@"OK" identifierKey:@"key" userInfo:userInfo soundName:sound region:nil regionTriggersOnce:YES category:nil];

[JPUSHService showLocalNotificationAtFront:local identifierKey:@"key"];

if (application.applicationState == UIApplicationStateActive) {

// 转换成一个本地通知,显示到通知栏,你也可以直接显示出一个alertView,只是那样稍显aggressive:)

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.userInfo = userInfo;

localNotification.soundName = UILocalNotificationDefaultSoundName;

localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];

//  收到本地通知 badgeNumber ++;

NSInteger badgeNumber = application.applicationIconBadgeNumber;

badgeNumber ++;

localNotification.applicationIconBadgeNumber = badgeNumber;

// ios8后,需要添加这个注册,才能得到授权

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type

categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

// 通知重复提示的单位,可以是天、周、月

localNotification.repeatInterval = NSCalendarUnitDay;

}

//        localNotification.

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

//Required

[JPUSHService handleRemoteNotification:userInfo];

completionHandler(UIBackgroundFetchResultNewData);

}
#pragma mark 获取自定义消息推送内容

- (void)networkDidReceiveMessage:(NSNotification *)notification

{

NSDictionary *userInfo = [notification userInfo];

//    NSString *content = [userInfo valueForKey:@"content"];

//    NSDictionary *extras = [userInfo valueForKey:@"extras"];

//    NSString *customizeField1 = [extras valueForKey:@"customizeField1"];

NSLog(@"dadasdasda");

NSLog(@"%@",userInfo);

}

进行处理,比如对badgeNumber的增减,也就是app外面的那个红色数字提示,以及本地通知的处理:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification   //  后台点击通知或者前台接受到通知时  会走

{

//  点击本地通知 badgeNumber --;

NSInteger badgeNumber = application.applicationIconBadgeNumber;

badgeNumber --;

application.applicationIconBadgeNumber = badgeNumber;

NSArray *localNotifications = application.scheduledLocalNotifications;

if (localNotifications.count >= 64) {

//  消息上限  删除所有的本地通知

[application cancelAllLocalNotifications];

application.applicationIconBadgeNumber = 0;

}

//  接收到了就删除 本地通知

[application cancelLocalNotification:notification];

//  重新设置 APP 的 BadgeNumber

NSArray *NowlocalNotifications = application.scheduledLocalNotifications;

application.applicationIconBadgeNumber = NowlocalNotifications.count;

}

相关文章

  • iOS 非常简单的 极光推送 -- LZ

    极光: https://www.jpush.cn 先申请 获取appKey; 2.程序入口中 添加代码(就是这...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • 极光推送

    极光推送视频地址,非常详细的极光推送视频 极光推送

  • iOS-iOS10极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • iOS-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • 极光推送的集成规则

    首先,iOS集成极光推送还是挺简单的。第一点,在APPdelegate中注册相关的极光推送信息,包括AppKey及...

  • 极光推送集成开发

    1.极光推送集成与设置 极光推送地址①注册极光推送账号。②在应用管理内按照步骤创建APP。③找到“文档——iOS—...

  • iOS —— 极光推送和极光IM

    前言 (环境:iOS12.0、极光推送SDK3.1.0、极光IM3.7.0) 写iOS 推送(苹果原生态)时,笔者...

  • 环信消息推送

    一,关于推送 之前做过 极光 APNS 个推的 推送 : 这里说下 极光推送是比较 适合用在 iOS 端和 安卓端...

  • 使用苹果原生APNS和后端推送工具Easy APNs Provi

    最近研究苹果原生apns,极光推送在此不详细解释了,具体查看极光文档极光推送传送门 原生APNS,iOS 代码如下...

网友评论

    本文标题:iOS 非常简单的 极光推送 -- LZ

    本文链接:https://www.haomeiwen.com/subject/nmlxrttx.html