diff --git a/doc/Common_detail_api.md b/doc/Common_detail_api.md index f9e997f..b60b1e3 100644 --- a/doc/Common_detail_api.md +++ b/doc/Common_detail_api.md @@ -370,3 +370,27 @@ ps:点击通知后传递的 json object 保存在 window.plugins.jPushPlugin.r "content":"今天去哪儿", "extras":{"key":"不填写没有"} } + +## 判断系统设置中是否允许当前应用推送 +### API - getUserNotificationSettings +判断系统设置中是否允许当前应用推送。 + +在 Android 中,返回值为 0 时,代表系统设置中关闭了推送;为 1 时,代表打开了推送(目前仅适用于Android 4.4+)。 + +在 iOS 中,返回值为 0 时,代表系统设置中关闭了推送;大于 0 时,代表打开了推送,且能够根据返回值判断具体通知形式: + + UIRemoteNotificationTypeNone = 0, // 0 + UIRemoteNotificationTypeBadge = 1 << 0, // 1 + UIRemoteNotificationTypeSound = 1 << 1, // 2 + UIRemoteNotificationTypeAlert = 1 << 2, // 4 + UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3 // 8 + +#### 代码示例 + + window.plugins.jPushPlugin.getUserNotificationSettings(function(result) { + if(result == 0) { + // 系统设置中已关闭应用推送。 + } else if(result > 0) { + // 系统设置中打开了应用推送。 + } + }); diff --git a/package.json b/package.json index 4c3cb88..7fe4873 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "jpush-phonegap-plugin", - "version": "2.2.1", + "version": "2.2.2", "description": "JPush for cordova plugin", "cordova": { - "id": "cn.jpush.phonegap.JPushPlugin", + "id": "cordova-plugin-JPush", "platforms": [ "ios", "android" diff --git a/plugin.xml b/plugin.xml index 14d42c6..3cbd014 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,8 +1,8 @@ + id="cordova-plugin-JPush" + version="2.2.2"> JPush Plugin JPush for cordova plugin @@ -16,8 +16,10 @@ + + - + @@ -31,7 +33,7 @@ - + @@ -58,6 +60,8 @@ + + @@ -136,6 +140,18 @@ + + + + + + + + + - diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index 727f799..ef65ef1 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -2,6 +2,7 @@ package cn.jpush.phonegap; import android.app.Activity; import android.content.Context; +import android.support.v4.app.NotificationManagerCompat; import android.text.TextUtils; import android.util.Log; @@ -26,7 +27,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import cn.jpush.android.api.BasicPushNotificationBuilder; -import cn.jpush.android.api.CustomPushNotificationBuilder; import cn.jpush.android.api.JPushInterface; import cn.jpush.android.api.TagAliasCallback; import cn.jpush.android.data.JPushLocalNotification; @@ -35,6 +35,7 @@ public class JPushPlugin extends CordovaPlugin { private final static List methodList = Arrays.asList( "addLocalNotification", + "areNotificationEnabled", "clearAllNotification", "clearLocalNotifications", "clearNotificationById", @@ -312,6 +313,18 @@ public class JPushPlugin extends CordovaPlugin { } } + void areNotificationEnabled(JSONArray data, final CallbackContext callback) { + NotificationManagerCompat nmc = NotificationManagerCompat.from( + cordova.getActivity().getApplicationContext()); + int isEnabled; + if (nmc.areNotificationsEnabled()) { + isEnabled = 1; + } else { + isEnabled = 0; + } + callback.success(isEnabled); + } + void setLatestNotificationNum(JSONArray data, CallbackContext callbackContext) { int num = -1; try { diff --git a/src/ios/Plugins/JPushPlugin.h b/src/ios/Plugins/JPushPlugin.h index 1164636..8ce48f4 100644 --- a/src/ios/Plugins/JPushPlugin.h +++ b/src/ios/Plugins/JPushPlugin.h @@ -58,6 +58,9 @@ //地理位置上报 [latitude,longitude] -(void)setLocation:(CDVInvokedUrlCommand*)command; +//检查用户的推送设置情况 +-(void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command; + /* * 以下为js中可监听到的事件 * jpush.openNotification 点击推送消息启动或唤醒app diff --git a/src/ios/Plugins/JPushPlugin.m b/src/ios/Plugins/JPushPlugin.m index c24ce44..a16c07f 100644 --- a/src/ios/Plugins/JPushPlugin.m +++ b/src/ios/Plugins/JPushPlugin.m @@ -52,7 +52,7 @@ static NSDictionary *_launchOptions = nil; }else{ result = @(1); } - [self hanleResultWithValue:result command:command]; + [self handleResultWithValue:result command:command]; } -(void)initial:(CDVInvokedUrlCommand*)command{ @@ -153,7 +153,7 @@ static NSDictionary *_launchOptions = nil; -(void)getRegistrationID:(CDVInvokedUrlCommand*)command{ NSString* registrationID = [JPUSHService registrationID]; NSLog(@"### getRegistrationID %@",registrationID); - [self hanleResultWithValue:registrationID command:command]; + [self handleResultWithValue:registrationID command:command]; } -(void)startLogPageView:(CDVInvokedUrlCommand*)command{ @@ -222,7 +222,7 @@ static NSDictionary *_launchOptions = nil; -(void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command { NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber; NSNumber *number = [NSNumber numberWithInteger:num]; - [self hanleResultWithValue:number command:command]; + [self handleResultWithValue:number command:command]; } -(void)setDebugModeFromIos:(CDVInvokedUrlCommand*)command{ @@ -259,6 +259,20 @@ static NSDictionary *_launchOptions = nil; [JPUSHService setLatitude:[((NSString*)command.arguments[0]) doubleValue] longitude:[((NSString*)command.arguments[1]) doubleValue]]; } +-(void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command{ + if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { + UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; + UIUserNotificationType type = settings.types; + NSNumber *number = [NSNumber numberWithInteger:type]; + [self handleResultWithValue:number command:command]; + }else{ + UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + NSNumber *number = [NSNumber numberWithInteger:type]; + [self handleResultWithValue:number command:command]; + } + +} + #pragma mark- 内部方法 +(void)setLaunchOptions:(NSDictionary *)theLaunchOptions{ _launchOptions = theLaunchOptions; @@ -298,7 +312,7 @@ static NSDictionary *_launchOptions = nil; } #pragma mark 将参数返回给js --(void)hanleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{ +-(void)handleResultWithValue:(id)value command:(CDVInvokedUrlCommand*)command{ CDVPluginResult *result = nil; CDVCommandStatus status = CDVCommandStatus_OK; diff --git a/src/ios/lib/JPUSHService.h b/src/ios/lib/JPUSHService.h index b0aeae7..968a700 100755 --- a/src/ios/lib/JPUSHService.h +++ b/src/ios/lib/JPUSHService.h @@ -9,7 +9,7 @@ * Copyright (c) 2011 ~ 2015 Shenzhen HXHG. All rights reserved. */ -#define JPUSH_VERSION_NUMBER 2.1.7 +#define JPUSH_VERSION_NUMBER 2.1.8 #import diff --git a/src/ios/lib/jpush-ios-2.1.7.a b/src/ios/lib/jpush-ios-2.1.8.a similarity index 85% rename from src/ios/lib/jpush-ios-2.1.7.a rename to src/ios/lib/jpush-ios-2.1.8.a index 9d312e0..3a34c19 100644 Binary files a/src/ios/lib/jpush-ios-2.1.7.a and b/src/ios/lib/jpush-ios-2.1.8.a differ diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index 64d273c..3689fab 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -210,6 +210,23 @@ JPushPlugin.prototype.getApplicationIconBadgeNumber = function(callback) { } } + +// 判断系统设置中是否对本应用启用通知。 +// iOS: 返回值如果大于 0,代表通知开启;0: 通知关闭。 +// UIRemoteNotificationTypeNone = 0, +// UIRemoteNotificationTypeBadge = 1 << 0, +// UIRemoteNotificationTypeSound = 1 << 1, +// UIRemoteNotificationTypeAlert = 1 << 2, +// UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3, +// Android: 返回值 1 代表通知启用、0: 通知关闭。 +JPushPlugin.prototype.getUserNotificationSettings = function(callback) { + if(this.isPlatformIOS()) { + this.call_native("getUserNotificationSettings", [], callback); + } else if (device.platform == "Android") { + this.call_native("areNotificationEnabled", [], callback); + } +} + // Android methods JPushPlugin.prototype.setDebugMode = function(mode) { if(device.platform == "Android") {