ionic整合極光推送外掛-iOS
阿新 • • 發佈:2019-02-02
DEMO地址https://github.com/JsonJieLi/cordova-jpushDemo/tree/master
1.首先建立一個ionic的專案 前面有介紹不詳細說了也可以參考官方網站
2安裝外掛
$ sudo ionic plugin add https://github.com/DongHongfei/jpush-phonegap-plugin.git
就安裝成功了
3.修改配置
- 修改
PushConfig.plist
修改對應的APP_KEY和CHANNEL(渠道)
4.在 js 中新增通知實現
- 在
app.js
最後新增
.factory('Push',function(){var push ;return{
setBadge:function(badge){if(push){
console.log('jpush: set badge', badge);
plugins.jPushPlugin.setBadge(badge);}},
setAlias:function(alias){if(push){
console.log('jpush: set alias',alias);
plugins.jPushPlugin.setAlias(alias);}},
check :function(){if(window.jpush && push){
plugins.jPushPlugin.receiveNotificationIniOSCallback(window.jpush);
window.jpush =null;}},
init:function(notificationCallback){
console.log('jpush: start init-----------------------');
push = window.plugins && window.plugins.jPushPlugin;if(push){
console.log('jpush: init');
plugins.jPushPlugin.init();
plugins.jPushPlugin.setDebugMode(true);
plugins.jPushPlugin.openNotificationInAndroidCallback = notificationCallback;
plugins.jPushPlugin.receiveNotificationIniOSCallback = notificationCallback;}}};});
在 app.js 的 run 函式裡定義通知回撥函式
記得在 run 函式裡引用 Push 先
// push notification callbackvar notificationCallback =function(data){
console.log('received data :'+ data);var notification = angular.fromJson(data);//app 是否處於正在執行狀態var isActive = notification.notification;// here add your code//iosif(ionic.Platform.isIOS()){
window.alert(notification);}else{//非 ios(android)}};
在 $ionicPlatform.ready 裡進行初始化
//初始化Push.init(notificationCallback);//設定別名Push.setAlias("12345678");
5.新增iOS的程式碼
- 修改 AppDelegate.m, 新增
#import "APService.h"#import "JPushPlugin.h" //viper
didFinishLaunchingWithOptions
函式中新增
// Required#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1if([[UIDevice currentDevice].systemVersion floatValue]>=8.0){//可以新增自定義categories[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert)
categories:nil];}else{//categories 必須為nil[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)
categories:nil];}#else//categories 必須為nil[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)
categories:nil];#endif// Required[APService setupWithOption:launchOptions];
didRegisterForRemoteNotificationsWithDeviceToken
中新增
// Required[APService registerDeviceToken:deviceToken];[APService setDebugMode];
新增函式
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {// Required[APService handleRemoteNotification:userInfo];
BOOL isActive;if(application.applicationState ==UIApplicationStateActive){
isActive = TRUE;}else{
isActive = FALSE;}NSDictionary*dict=[[NSMutableDictionary alloc] initWithDictionary:userInfo];[dict setValue:[[NSNumber alloc] initWithBool:isActive] forKey:@"isActive"];[[NSNotificationCenter defaultCenter] postNotificationName:kJPushPlugReceiveNotificaiton
object:dict];//viper}
6.修改專案 Capabilities,開啟 Background Modes,勾選最後一項Remote notications
參考了http://ionichina.com/topic/54fab88b7b505d9b1b5573a6謝謝,圖文結合更詳細