美文网首页
iOS极光推送功能

iOS极光推送功能

作者: 进阶的蚊子 | 来源:发表于2017-07-11 15:50 被阅读4065次

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

第一步 当然是证书的问题,这里我就直接以开发证书为例,如果是生产证书,就直接自己再生成一个生产证书,搞成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"];
    }
    
      }
   }

相关文章

  • iOS极光推送功能

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

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

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

  • iOS-iOS10极光推送的使用

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

  • iOS-极光推送的使用

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

  • ReactNative开发-集成极光推送 jpush-react

    前言 极光推送官方有提供支持React Native版本的插件(ios和android),可以快速集成推送功能。目...

  • 极光推送集成开发

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

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

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

  • iOS推送通知(极光推送)

    写在前面 要实现推送功能先要有苹果的推送证书:如何创建苹果推送证书要使用极光推送的功能,需要先集成极光推送SDK ...

  • iOS开发 - 友盟推送

    在iOS 开发中项目会集成推送功能, 推送的三方很多,如友盟,极光推送.他们的推送原理大致差不多,文档也写的很详细...

  • ios 马甲包上架

    应公司发展需要,先大量招收APP开发和上架苹果商店技术 ios H5 功能极光推送 网页跳转 umeng推送 ...

网友评论

      本文标题:iOS极光推送功能

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