iOS极光推送功能

最近想把自己写的极光推送遇到的一些问题列在这里,以便那些遇到推送问题的砖友们跳出这些坑.

第一步 当然是证书的问题,这里我就直接以开发证书为例,如果是生产证书,就直接自己再生成一个生产证书,搞成P12文件,提交到极光推送的控制台,需要注意的是,推送证书,开发证书,profile使用的证书必须一致.尽管在xcode8 模式下 ,xcode自身就有管理证书的功能了.但是这个测试和生产证书还是要自己去开发者中心生成导出的.
第二步 导入sdk

3090071-371596d316edbdde.png

第三步 需要导入各种依赖库

3090071-8c949b2daaa8f044.png

第四步 进入项目的appdelegate里面,首先导入头文件和遵循代理

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];
    //设置红色角标
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [JPUSHService setBadge:0];

     }
    

第六步 实现通知和协议方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//发送通知
[[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); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
  }

   // 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();  // 系统要求执行这个方法
    }

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"]; //服务端传递的Extras附加字段,key是自己定义的
    DDLog(@"----userInfo---%@",userInfo);
//发送通知
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showRedBadge" object:nil userInfo:userInfo];
}
 - (void)networkDidLogin:(NSNotification *)notification 
  {
         NSLog(@"已登录");
         if ([JPUSHService registrationID]) {
    
    //下面是我拿到registeID,发送给服务器的代码,可以根据你需求来处理
    NSString *registerid = [JPUSHService registrationID];
    
    NSLog(@"APPDelegate开始上传rgeisterID---%@",registerid);
    if(registerid)
    {
      [[NSUserDefaults standardUserDefaults] setObject:registerid forKey:@"registerid"];
    }
    
      }
   }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容