極光征文 | iOS與Android極光推送實戰(zhàn)

iOS篇 未完待續(xù)

  1. 導(dǎo)入框架
  • Cocoapods 導(dǎo)入
pod 'JPush', '3.1.0'
  • 手動導(dǎo)入(比較麻煩,不是很推薦)
    1. 在極光官網(wǎng)下載最新 SDK
    2. 將 SDK 包解壓,在 Xcode 中選擇 “Add files to 'Your project name'...”,將解壓后的 lib 子文件夾(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目錄中。
      3.添加 Framework
      • CFNetwork.framework
      • CoreFoundation.framework
      • CoreTelephony.framework
      • SystemConfiguration.framework
      • CoreGraphics.framework
      • Foundation.framework
      • UIKit.framework
      • Security.framework
      • libz.tbd
      • UserNotifications.framework
      • libresolv.tbd

注意: 如果集成 JPush 3.0.1 及以上版本, 且同時集成極光其他 SDK(如:JMessage 3.0.0 及以上版本)
1. Cocoapods 導(dǎo)入,建議都更新為線上最新版本,來避免 JCore 版本不一致導(dǎo)致的沖突。
2. 手動導(dǎo)入,在工程中只需保留一個最新版本的 jcore-ios-x.x.x.a 靜態(tài)庫文件。

  1. 開啟Target 的 Capabilities->Push Notifications 選項,如圖:


    1.jpg
  2. 在AppDelegate.m里引入
// 引入 JPush 功能所需頭文件
#import "JPUSHService.h"
// iOS10 注冊 APNs 所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
  1. 初始化
    請將以下代碼添加到didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里
    JpushAppKey是你申請的key
    這邊是大家經(jīng)常會犯錯的地方,apsForProduction:NO表示采用的是開發(fā)證書,YES表示采用生產(chǎn)證書發(fā)布應(yīng)用
  //初始化APNs 
  JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
  [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  //初始化JPush 
  #ifdef DEBUG
  [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:NO];
  #else
  [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:YES];
  #endif
  1. 為 AppDelegate 添加 Delegate(JPUSHRegisterDelegate) 并添加回調(diào)方法
@interface AppDelegate ()<JPUSHRegisterDelegate>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Required - 注冊 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}
@end
  1. 添加處理 APNs 通知回調(diào)方法
#pragma mark- JPUSHRegisterDelegate

// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
  if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //從通知界面直接進入應(yīng)用
  }else{
    //從通知設(shè)置界面進入應(yīng)用
  }
}

// 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í)行這個方法
}

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

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

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

  // Required, For systems with less than or equal to iOS 6
  [JPUSHService handleRemoteNotification:userInfo];
}
  1. 上傳registrationID給服務(wù)器端(也可以設(shè)置別名,設(shè)置別名也在這里寫)
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        ZPLog(@"------>registrationID: %@",[JPUSHService registrationID]);
        //上傳極光registrationID給服務(wù)器
 }];

公司推送后臺

  1. 標簽類型
  • 項目
  • 性別
  • 年齡
  • 用戶類型


    1.png
  1. 查看通知后的動作
  • 默認打開app
  • 打開app里的某個模塊
  • 打開網(wǎng)頁


    2.png
思路:本來是想App 用戶登錄成功后,后臺返回改用戶的標簽數(shù)組給我,然后我調(diào)用此 API 來設(shè)置標簽.但是發(fā)現(xiàn)這些標簽中有個歲數(shù),假設(shè)用戶登錄后,一直不退出,那他的標簽里的歲數(shù)一直就沒變. 商量后,由我App這邊自己組裝好和后臺約定的用戶的標簽數(shù)組,然后每次打開App,都設(shè)置一下標簽
/**
 覆蓋tags
 調(diào)用該接口會覆蓋用戶所有的tags

 @param tags 需要設(shè)置的tags集合
 @param completion 響應(yīng)回調(diào)
 @param seq 請求序列號
 */
+ (void)setTags:(NSSet<NSString *> *)tags
     completion:(JPUSHTagsOperationCompletion)completion
            seq:(NSInteger)seq;

常見收不到通知的原因

  • 環(huán)境不對,先檢查客戶端的方法
    初始化代碼中 apsForProduction:isProduction 的值需要與客戶端的證書環(huán)境保持一致
  • 后臺Api推送的話再去看后臺的,后臺也有一個配置生產(chǎn)還是測試環(huán)境的屬性.
  • 前后端的appkey不一致

Android篇

如果你找不到原因,建議你可以加qq群445928015,極光有專業(yè)的客服工作日在線答疑.我使用過好多三方服務(wù),第一次見不付費的非VIP用戶也會被客服耐心解答的,經(jīng)常遇到相同的問題客服也是非常有耐心,非常難得.

本文為極光征文參賽文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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