日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

极光推送集成

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极光推送集成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

pod 集成 的?

pod 'JPush'


頭文件導入

#import "JPUSHService.h"

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import <UserNotifications/UserNotifications.h>


代理方法?

@interface AppDelegate ()<JPUSHRegisterDelegate>

添加初始化代碼:

? ??// ? 極光推送

? ? // 3.0.0及以后版本注冊可以這樣寫,也可以繼續用舊的注冊方式

? ? JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];

? ? entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;

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

? ? ? ? //可以添加自定義categories

? ? ? ? //? ? if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {

? ? ? ? //? ? ? NSSet<UNNotificationCategory *> *categories;

? ? ? ? //? ? ? entity.categories = categories;

? ? ? ? //? ? }

? ? ? ? //? ? else {

? ? ? ? //? ? ? NSSet<UIUserNotificationCategory *> *categories;

? ? ? ? //? ? ? entity.categories = categories;

? ? ? ? //? ? }

? ? }

? ? [JPUSHService registerForRemoteNotificationConfig:entity delegate:(id)self];


?? NSString *advertisingId = [DeviceInformation getIDFA];

? ? //如不需要使用IDFAadvertisingIdentifier 可為nil

? ? [JPUSHService setupWithOption:launchOptions appKey:“你的APPKey” channel:@"推廣渠道" apsForProduction:@“是否發布版”?advertisingIdentifier:advertisingId];

?? ?

? ? //2.1.9版本新增獲取registration id block接口。

? ? [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {

? ? ? ? if(resCode == 0){

? ? ? ? ? ? NSLog(@"registrationID獲取成功:%@",registrationID);?

? ? ? ? }

? ? ? ? else{

? ? ? ? ? ? NSLog(@"registrationID獲取失敗,code%d",resCode);

? ? ? ? }

? ? }];



- (void)applicationWillResignActive:(UIApplication *)application {

? ? //? ? [APService stopLogPageView:@"aa"];

? ? // Sent when the application is about to move from active to inactive state.

? ? // This can occur for certain types of temporary interruptions (such as an

? ? // incoming phone call or SMS message) or when the user quits the application

? ? // and it begins the transition to the background state.

? ? // Use this method to pause ongoing tasks, disable timers, and throttle down

? ? // OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

? ? // Use this method to release shared resources, save user data, invalidate

? ? // timers, and store enough application state information to restore your

? ? // application to its current state in case it is terminated later.

? ? // If your application supports background execution, this method is called

? ? // instead of applicationWillTerminate: when the user quits.

?? ?

? ? //[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];

?? ?

? ? [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

? ? [application setApplicationIconBadgeNumber:0];

? ? [application cancelAllLocalNotifications];

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

? ? // Restart any tasks that were paused (or not yet started) while the

? ? // application was inactive. If the application was previously in the

? ? // background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

? ? // Called when the application is about to terminate. Save data if

? ? // appropriate. See also applicationDidEnterBackground:.

}

注冊上報 DeviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

?

? ? NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);

? ? [JPUSHService registerDeviceToken:deviceToken];

}

通知失敗方法

- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

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

}

通知回調方法

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1

- (void)application:(UIApplication *)application

didRegisterUserNotificationSettings:

(UIUserNotificationSettings *)notificationSettings {

}


// Called when your app has been activated by the user selecting an action from

// a local notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling

// the action.

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification? completionHandler:(void (^)())completionHandler {

}


// Called when your app has been activated by the user selecting an action from

// a remote notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling

// the action.

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {

}

#endif


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

? ? [JPUSHService handleRemoteNotification:userInfo];

? ? NSLog(@"iOS6及以下系統,收到通知:%@", [self logDic:userInfo]);


}


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

? ? [JPUSHService handleRemoteNotification:userInfo];

? ? NSLog(@"iOS7及以上系統,收到通知:%@", [self logDic:userInfo]);

?? ?

? ? if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {

?? ? ? ?

? ? }

?? ?

? ? completionHandler(UIBackgroundFetchResultNewData);

}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

? ? [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];

}


#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#pragma mark- JPUSHRegisterDelegate

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

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

?? ?

? ? UNNotificationRequest *request = notification.request; // 收到推送的請求

? ? UNNotificationContent *content = request.content; // 收到推送的消息內容

?? ?

? ? NSNumber *badge = content.badge;? // 推送消息的角標

? ? NSString *body = content.body;? ? // 推送消息體

? ? UNNotificationSound *sound = content.sound;? // 推送消息的聲音

? ? NSString *subtitle = content.subtitle;? // 推送消息的副標題

? ? NSString *title = content.title;? // 推送消息的標題

?? ?

? ? if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

? ? ? ? [JPUSHService handleRemoteNotification:userInfo];

? ? ? ? NSLog(@"iOS10 前臺收到遠程通知:%@", [self logDic:userInfo]);

?? ? ? ?

? ? }

? ? else {

? ? ? ? // 判斷為本地通知

? ? ? ? NSLog(@"iOS10 前臺收到本地通知:{\nbody:%@\ntitle:%@,\nsubtitle:%@,\nbadge%@\nsound%@\nuserInfo%@\n}",body,title,subtitle,badge,sound,userInfo);

? ? }

? ? completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有BadgeSoundAlert三種類型可以設置

}


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

?? ?

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

? ? UNNotificationRequest *request = response.notification.request; // 收到推送的請求

? ? UNNotificationContent *content = request.content; // 收到推送的消息內容

?? ?

? ? NSNumber *badge = content.badge;? // 推送消息的角標

? ? NSString *body = content.body;? ? // 推送消息體

? ? UNNotificationSound *sound = content.sound;? // 推送消息的聲音

? ? NSString *subtitle = content.subtitle;? // 推送消息的副標題

? ? NSString *title = content.title;? // 推送消息的標題

?? ?

? ? if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

? ? ? ? [JPUSHService handleRemoteNotification:userInfo];

? ? ? ? NSLog(@"iOS10 收到遠程通知:%@", [self logDic:userInfo]);

?? ? ? ?

? ? }

? ? else {

? ? ? ? // 判斷為本地通知

? ? ? ? NSLog(@"iOS10 收到本地通知:{\nbody:%@\ntitle:%@,\nsubtitle:%@,\nbadge%@\nsound%@\nuserInfo%@\n}",body,title,subtitle,badge,sound,userInfo);

? ? }

?? ?

? ? completionHandler();? // 系統要求執行這個方法

}

#endif


// log NSSet with UTF8

// if not ,log will be \Uxxx

- (NSString *)logDic:(NSDictionary *)dic {

? ? if (![dic count]) {

? ? ? ? return nil;

? ? }

? ? NSString *tempStr1 = [[dic description] stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];

? ? NSString *tempStr2 =? [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

? ? NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];

? ? NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];

? ? NSString *str = [NSPropertyListSerialization propertyListFromData:tempData? mutabilityOption:NSPropertyListImmutable? format:NULL errorDescription:NULL];

? ? return str;

}


成功運行:

--------------------------- JPush Log ----------------------------

--------------------JPush SDK Version:3.0.3--build:34----------

--------------------JCore Lib Version:1.1.2--build:18----------

-----------------AppKey:**********************----------------

----------------------------------------------------------------

2017-05-03 11:08:12.839412+0800 MedChannel[7645:3068629]? | JIGUANG | I - [JIGUANGClientController] Action - jpush setup

2017-05-03 11:08:12.856833+0800 MedChannel[7645:3068629]? | JIGUANG | I - [JIGUANGClientController] Action - setup

<AppDelegate.m 78>:-[AppDelegate application:didFinishLaunchingWithOptions:]_block_invoke NSLog:

? registrationID獲取成功:13165ffa4e38f901e23

-----

<AppDelegate.m 233>:-[AppDelegate application:didRegisterForRemoteNotificationsWithDeviceToken:] NSLog:

? Device Token: <d7b48a07 d97f4324 3c2d44fa e9816d0a 99492c77 4e42c8e7 9e0c3f5b f66c3dcf>

-----

2017-05-03 11:08:17.571764+0800 MedChannel[7645:3068623]? | JIGUANG | I - [JIGUANGSessionController] connecting with coreAddr 139.198.0.239,port 7001

2017-05-03 11:08:21.049578+0800 MedChannel[7645:3068629]? | JIGUANG | I - [JIGUANGLogin]?

----- login result -----

uid:9283689888?

registrationID:13165ffa4e38f901e23


詳細內容看極光官方文檔



總結

以上是生活随笔為你收集整理的极光推送集成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。