iOS極光推送功能

最近想把自己寫的極光推送遇到的一些問題列在這里,以便那些遇到推送問題的磚友們跳出這些坑.

第一步 當(dāng)然是證書的問題,這里我就直接以開發(fā)證書為例,如果是生產(chǎn)證書,就直接自己再生成一個生產(chǎn)證書,搞成P12文件,提交到極光推送的控制臺,需要注意的是,推送證書,開發(fā)證書,profile使用的證書必須一致.盡管在xcode8 模式下 ,xcode自身就有管理證書的功能了.但是這個測試和生產(chǎn)證書還是要自己去開發(fā)者中心生成導(dǎo)出的.
第二步 導(dǎo)入sdk

3090071-371596d316edbdde.png

第三步 需要導(dǎo)入各種依賴庫

3090071-8c949b2daaa8f044.png

第四步 進(jìn)入項(xiàng)目的appdelegate里面,首先導(dǎo)入頭文件和遵循代理

import "AppDelegate.h"
import "JPUSHService.h"
ifdef NSFoundationVersionNumber_iOS_9_x_Max
import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()<JPUSHRegisterDelegate>

第五步 在didFinishLaunchingWithOptions方法中配置

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    [self jpushInitWith:launchOptions];
    }

  • (void)jpushInitWith:(NSDictionary)launchOptions
    {
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    //生成idfa 字段
    NSString
    advertisingId=[SimulateIDFA createSimulateIDFA];
    DDLog(@"----ifda----%@",advertisingId);
    if (advertisingId) {
    [[NSUserDefaults standardUserDefaults] setObject:advertisingId forKey:@"advertisingId"];
    }
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    [defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil];
    [JPUSHService setupWithOption:launchOptions appKey:appKey
    channel:channel
    apsForProduction:isProduction
    advertisingIdentifier:advertisingId];
    //設(shè)置紅色角標(biāo)
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [JPUSHService setBadge:0];

     }
    

第六步 實(shí)現(xiàn)通知和協(xié)議方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"showRedBadge" object:nil userInfo:userInfo];

    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
  }

   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
       DDLog(@"----userinfo---%@----",userInfo);
    // Required,For systems with less than or equal to iOS6
     [JPUSHService handleRemoteNotification:userInfo];
   }
   - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
       //Optional
       NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }
     - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
   {
         [JPUSHService registerDeviceToken:deviceToken];
  }

   #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.trigger isKindOfClass:[UNPushNotificationTrigger class]])        {
       [JPUSHService handleRemoteNotification:userInfo];
   }
     completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置
  }

   // 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.trigger isKindOfClass:  [UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系統(tǒng)要求執(zhí)行這個方法
    }

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"]; //服務(wù)端傳遞的Extras附加字段,key是自己定義的
    DDLog(@"----userInfo---%@",userInfo);
//發(fā)送通知
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showRedBadge" object:nil userInfo:userInfo];
}
 - (void)networkDidLogin:(NSNotification *)notification 
  {
         NSLog(@"已登錄");
         if ([JPUSHService registrationID]) {
    
    //下面是我拿到registeID,發(fā)送給服務(wù)器的代碼,可以根據(jù)你需求來處理
    NSString *registerid = [JPUSHService registrationID];
    
    NSLog(@"APPDelegate開始上傳rgeisterID---%@",registerid);
    if(registerid)
    {
      [[NSUserDefaults standardUserDefaults] setObject:registerid forKey:@"registerid"];
    }
    
      }
   }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容