极光推送官方版的 phonegap 插件在这里。由于官方版插件 ios 版暂时没有打开通知的方法,所以在官方基础上修改了下,修改后的插件放在了这里,下面说明以修改后的插件为准。(感谢极光官方大神viper耐心帮助,同时也参考了下@lanceli大神的cnodejs-ionic项目)
极光账户设置部分可以参考小和尚的这篇分享。
下面主要说明项目代码部分修改。
$ ionic start --id com.ionichina.ionicjpush ionic_jpush tabs
注:修改 id 为自己应用的 Bundle identifier
$ cd ionic_jpush
$ ionic platform add ios
$ ionic plugin add https://github.com/DongHongfei/jpush-phonegap-plugin.git
等待时间比较长,你也可以像小和尚文章里介绍的先下载下来,再安装,但这个过程是跑不了的
(接下来,蛋疼的事情开始了)
ionic_jpush\plugins\cn.jpush.phonegap.JPushPlugin\src\ios\PushConfig.plist
修改对应的APP_KEY和CHANNEL(渠道)注意确保有如下代码,不然后面 Xcode 运行会警告:
APS_FOR_PRODUCTION
0
app.js
最后添加一个 push 工厂(参考了@lanceli 大神的Ccnodejs-ionic项目) .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 callback
var notificationCallback = function(data) {
console.log('received data :' + data);
var notification = angular.fromJson(data);
//app 是否处于正在运行状态
var isActive = notification.notification;
// here add your code
//ios
if (ionic.Platform.isIOS()) {
window.alert(notification);
} else {
//非 ios(android)
}
};
在 $ionicPlatform.ready 里进行初始化
//初始化
Push.init(notificationCallback);
//设置别名
Push.setAlias("12345678");
$ ionic build ios
(接下来,更蛋疼的事情开始了)
#import "APService.h"
#import "JPushPlugin.h" //viper
didFinishLaunchingWithOptions
函数中添加
// Required
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
if ([[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
}
接下就是设置一些Xcode常规操作,编译运行,从极光官方控制台发送一条通知,然后查看Xcode控制台,应该就会有推送的通知数据打印了。下面的事儿你自己应该搞的定。
示例代码我放在了这里。