1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java极光推送ios设置通知标题 iOS 极光推送接受通知和自定义消息及静默推送

java极光推送ios设置通知标题 iOS 极光推送接受通知和自定义消息及静默推送

时间:2019-04-25 08:32:39

相关推荐

java极光推送ios设置通知标题 iOS  极光推送接受通知和自定义消息及静默推送

由于配置证书和注册极光应用配置网上已经很多了所以在这里就不在多说了!!!

1.首先我们要先将从极光官网上下载来的最新的sdk中的lib文件夹导入工程中;

顺便添加一些依赖库如下:

/**添加依赖库

CFNetwork.framework

CoreFoundation.framework

CoreTelephony.framework

SystemConfiguration.framework

CoreGraphics.framework

Foundation.framework

UIKit.framework

Security.framework

libz.tbd (Xcode7以下版本是libz.dylib)

UserNotifications.framework (Xcode8及以上)

libresolv.tbd

*/

记得在工程中打开如下选项:

这些度干完之后可以先编译一下代码看是否会出现错误,然后我们就开始实现极光推送代码了.

2.代码实现

我们可以把AppDelegate 这个类加一个分类,这样看起来方便简洁,而且下一次使用的时候还可以重复使用.

下面是扩展后的代码如下:

下面这一段代码是 .h文件的内容,读者可以将他们复制到你新建的扩展类就好

#import"AppDelegate.h"

//引入JPush功能所需头文件

#import"JPUSHService.h"

// iOS10注册APNs所需头文件

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import

#endif

//如果需要使用idfa功能所需要引入的头文件(可选)

#import

@interfaceAppDelegate (Jpush)

/**

初始化Jpush

@param launchOptions launchOptions

*/

- (void)initJpush:(NSDictionary*)launchOptions;

/**

清除badge值

*/

- (void)cleanBadge;

@end

---------------------------------以下为.m文件内容---------------------------------------

#import"AppDelegate+Jpush.h"

#define app_Key @"0c6b42b1864207bd22d1a752"

#define channel_y @"Publish channel"

#define is_Production FALSE

@implementationAppDelegate (Jpush)

- (void)initJpush:(NSDictionary*)launchOptions{

//Required

//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式

JPUSHRegisterEntity* entity = [[JPUSHRegisterEntityalloc]init];

entity.types=JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert;

if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {

}

[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];

// Optional

//获取IDFA

//如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值

NSString*advertisingId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];

[JPUSHServicesetupWithOption:launchOptionsappKey:JPushKey

channel:channel_y

apsForProduction:is_Production

advertisingIdentifier:advertisingId];

//添加下面这个观察者是为了收取极光推送自定义消息的,这个通知的添加位置是随意的,即在你想要处理接收到自定义的消息的任何地方度可以调用,如果在除了本类外调用的话可能需要导入#import"JPUSHService.h"的头文件.并且实现以下networkDidReceiveMessage:方法就行了

//kJPFNetworkDidReceiveMessageNotification 他是接收自定义消息通知的name

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

}

/**

清除badge值

*/

- (void)cleanBadge{

[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];

[JPUSHServicesetBadge:0];

}

/**

注册DeviceToken

*/

- (void)application:(UIApplication*)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

/// Required -注册DeviceToken

[JPUSHServiceregisterDeviceToken:deviceToken];

}

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

//Optional

NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {

// Required,For systems with less than or equal to iOS6

[JPUSHServicehandleRemoteNotification:userInfo];

}

#pragma mark- JPUSHRegisterDelegate

// iOS 10 Support应用处于前台的状态下收到推送消息会调用此方法

- (void)jpushNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(NSInteger))completionHandler {

// Required

NSDictionary* userInfo = notification.request.content.userInfo;

if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

[JPUSHServicehandleRemoteNotification:userInfo];

}

completionHandler(UNNotificationPresentationOptionAlert);//需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置

NSLog(@"t三种类型可以选择设置%@",userInfo);

}

// iOS 10 Support当收到推送通知后打开推送消息会调用此方法

- (void)jpushNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler {

// Required

NSDictionary* userInfo = response.notification.request.content.userInfo;

if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

[JPUSHServicehandleRemoteNotification:userInfo];

}

completionHandler();//系统要求执行这个方法

//取得APNs标准信息内容

NSDictionary*aps = [userInfovalueForKey:@"aps"];

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

NSLog(@"系统要求执行这个方法%@",content);

}

//iOS7及以上系统,收到通知 此方法在接收到静默推送的时候会调用

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

// Required, iOS 7 Support

[JPUSHServicehandleRemoteNotification:userInfo];

//取得APNs标准信息内容

NSDictionary*aps = [userInfovalueForKey:@"aps"];

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

NSLog(@"系统要求执行这个方法%@",content);

completionHandler(UIBackgroundFetchResultNewData);

//[self tongzhi];

}

以上为全部实现代码 ,在使用的时候只需要在相应位置调用方法就行了,调用代码如下图:

此图为初始化极光调用方法

此图为显示和取消接受通知的角标

就这样你就可以顺利的接收到极光推送的消息了!!!!

3.如果要接受静默推送的话还需要在工程中打开如下选项:

配置选项

此文仅为个人见解,如我更好的建议可以随时联系,谢谢大家的阅读!!!!!!!!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。