Skip to content

Commit

Permalink
Merge pull request #38 from jpush/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
huangminlinux committed Jul 17, 2018
2 parents 2e1a7e5 + eec6a5b commit 70b1532
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
47 changes: 47 additions & 0 deletions API.md
Expand Up @@ -19,6 +19,7 @@
- [获取点击通知内容缓存](#获取点击通知内容缓存)
- [清除点击通知内容缓存](#清除点击通知内容缓存)
- [iOS API](#ios-api)
- [获取点击推送启动应用的 notification](#获取点击推送启动应用的-notification)
- [设置 Badge](#设置-badge)
- [本地通知](#本地通知)
- [日志等级设置](#日志等级设置)
Expand Down Expand Up @@ -652,6 +653,52 @@ window.plus.Push.clearLaunchAppCacheNotification();

## iOS API

### 获取点击推送启动应用的-notification

点击推送启动应用时 JS 还没加载,无法监听事件,该方法用于获取缓存的通知。

#### 代码示例

```javascript
plus.Push.getLaunchAppCacheNotification(
function (notification) {
alert(JSON.stringify(notification));
})


```

#### 回调参数:

notification 可以是本地推送也可以是远程推送(两种推送字段不太一样)。如果应用不是通过点击推送启动的 event 将返回 {} 空对象。

```
//远程推送格式
notification == {
_j_business: 1,
_j_uid: 1831789116,
_j_msgid: 3124434324,
aps: {
alert: "2423",
badge: 1,
sound: "default"
}
}
```

```
//本地推送格式
notification == {
content: "the content",
badge: 1,
extras: {
}
}
```



### 设置 Badge

#### API - setBadge, resetBadge
Expand Down
Expand Up @@ -34,8 +34,8 @@

@interface JPushPlugin()

@property(nonatomic,strong)PGMethod *aliasAndTagsCommand;

@property(nonatomic, strong)PGMethod *aliasAndTagsCommand;
@property(nonatomic, strong)NSDictionary *cacheLaunchNotification;
@end

@implementation JPushPlugin
Expand All @@ -44,7 +44,9 @@ @implementation JPushPlugin

- (void)onAppStarted:(NSDictionary*)options
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:[options description] delegate:nil cancelButtonTitle:@"fd" otherButtonTitles:nil, nil];
[alert show];
_cacheLaunchNotification = options;
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
NSString *rid = registrationID?:@"";
[self fireEvent: kJPushOnRegistrationId args: rid];
Expand Down Expand Up @@ -186,6 +188,33 @@ -(void)getRegistrationID:(PGMethod*)command{
}
}

-(void)getLaunchAppCacheNotification:(PGMethod*)command{
if (command) {

if (_cacheLaunchNotification != nil) {

if ([_cacheLaunchNotification valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[_cacheLaunchNotification valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
[self handleResultWithValue:dic command:command];
return;
}

if ([_cacheLaunchNotification valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *localNotification = [_cacheLaunchNotification valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSMutableDictionary *localNotificationEvent = @{}.mutableCopy;
localNotificationEvent[@"content"] = localNotification.alertBody;
localNotificationEvent[@"badge"] = @(localNotification.applicationIconBadgeNumber);
localNotificationEvent[@"extras"] = localNotification.userInfo;
[self handleResultWithValue:localNotificationEvent command:command];
return;
}

[self handleResultWithValue:@{} command:command];
}
}
}


-(void)handleResultWithValue:(id)value command:(PGMethod*)command{

PDRPluginResult *result = nil;
Expand Down
Expand Up @@ -67,6 +67,11 @@ document.addEventListener('plusready', function () {
isPushStopped: function (successCallback) {
this.callNative('isPushStopped', null, successCallback)
},
getCacheLaunchNotification: function (successCallback) {
//点击推送的通知启动应用 JS 还没 ready,无法获取事件,该方法用于获取缓存的通知。
this.callNative('getCacheLaunchNotification', null, successCallback)
},

// Android methods
init: function () {
if (plus.os.name == 'Android') {
Expand Down Expand Up @@ -140,7 +145,19 @@ document.addEventListener('plusready', function () {
}
},
onGetRegistrationId: function (rId) {
this.fireDocumentEvent('jpush.onGetRegistrationId', rId)
if (plus.os.name == 'Android') {
this.fireDocumentEvent('jpush.onGetRegistrationId', rId)
}
},
getLaunchAppCacheNotification: function (successCallback) {
if (plus.os.name == 'iOS') {
this.callNative('getLaunchAppCacheNotification', null, successCallback)
}
},
clearLaunchAppCacheNotification: function () {
if (plus.os.name == 'Android') {
this.callNative('clearLaunchAppCacheNotification', null, null)
}
},
receiveMessageInAndroidCallback: function (data) {
if (plus.os.name == 'Android') {
Expand All @@ -158,6 +175,14 @@ document.addEventListener('plusready', function () {
this.fireDocumentEvent('jpush.openNotification', this.openNotification)
}
},
openNotificationIniOSCallback: function (data) {
if (plus.os.name == 'iOS') {
data = JSON.stringify(data)
var jsonObj = JSON.parse(data)
this.openNotification = jsonObj
this.fireDocumentEvent('jpush.openNotification', this.openNotification)
}
},
receiveNotificationInAndroidCallback: function (data) {
if (plus.os.name == 'Android') {
data = JSON.stringify(data)
Expand All @@ -171,17 +196,10 @@ document.addEventListener('plusready', function () {
data = JSON.stringify(data)
var jsonObj = JSON.parse(data)
this.receiveNotification = jsonObj

this.fireDocumentEvent('jpush.receiveNotification', this.receiveNotification)
}
},
openNotificationIniOSCallback: function (data) {
if (plus.os.name == 'iOS') {
data = JSON.stringify(data)
var jsonObj = JSON.parse(data)
this.openNotification = jsonObj
this.fireDocumentEvent('jpush.openNotification', this.openNotification)
}
},
receiveMessageIniOSCallback: function (data) {
if (plus.os.name == 'iOS') {
data = JSON.stringify(data)
Expand Down Expand Up @@ -267,6 +285,7 @@ document.addEventListener('plusready', function () {
},
addLocalNotificationIniOS: function (delayTime, content, badge, notificationID, extras) {
if (plus.os.name == 'iOS') {
alert('fdsa')
var data = [delayTime, content, badge, notificationID, extras]
this.callNative('setLocalNotification', data, null)
}
Expand All @@ -286,4 +305,4 @@ document.addEventListener('plusready', function () {

JPushPlugin.init()
window.plus.Push = JPushPlugin
}, true)
}, true)
7 changes: 6 additions & 1 deletion jpush.js
Expand Up @@ -67,6 +67,11 @@ document.addEventListener('plusready', function () {
isPushStopped: function (successCallback) {
this.callNative('isPushStopped', null, successCallback)
},
getCacheLaunchNotification: function (successCallback) {
//点击推送的通知启动应用 JS 还没 ready,无法获取事件,该方法用于获取缓存的通知。
this.callNative('getCacheLaunchNotification', null, successCallback)
},

// Android methods
init: function () {
if (plus.os.name == 'Android') {
Expand Down Expand Up @@ -145,7 +150,7 @@ document.addEventListener('plusready', function () {
}
},
getLaunchAppCacheNotification: function (successCallback) {
if (plus.os.name == 'Android') {
if (plus.os.name == 'iOS') {
this.callNative('getLaunchAppCacheNotification', null, successCallback)
}
},
Expand Down

0 comments on commit 70b1532

Please sign in to comment.