From 35d4bf035c9c1f19bd57b38e26d701c3d34c8cfe Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Tue, 26 Sep 2017 19:36:06 -0400 Subject: [PATCH] requestPermissions for android --- .../evollu/react/fcm/FIRMessagingModule.java | 90 ++++++++++--------- .../react/fcm/SendNotificationTask.java | 31 +++---- 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index 10f7c73..788a314 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -24,6 +24,7 @@ import com.google.firebase.messaging.RemoteMessage.Notification; import android.app.Application; import android.os.Bundle; +import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import android.content.Context; @@ -57,7 +58,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li @ReactMethod public void getInitialNotification(Promise promise){ Activity activity = getCurrentActivity(); - if(activity == null || activity.getIntent().getAction() == "android.intent.action.MAIN"){ + if(activity == null || activity.getIntent().getAction().equals("android.intent.action.MAIN")){ promise.resolve(null); return; } @@ -65,17 +66,22 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li } @ReactMethod - public void requestPermissions(){ + public void requestPermissions(Promise promise){ + if(NotificationManagerCompat.from(getReactApplicationContext()).areNotificationsEnabled()){ + promise.resolve(true); + } else { + promise.reject(null, "Notification disabled"); + } } @ReactMethod public void getFCMToken(Promise promise) { try { - Log.d(TAG, "Firebase token: " + FirebaseInstanceId.getInstance().getToken()); - promise.resolve(FirebaseInstanceId.getInstance().getToken()); + Log.d(TAG, "Firebase token: " + FirebaseInstanceId.getInstance().getToken()); + promise.resolve(FirebaseInstanceId.getInstance().getToken()); } catch (Throwable e) { - e.printStackTrace(); - promise.reject(null,e.getMessage()); + e.printStackTrace(); + promise.reject(null,e.getMessage()); } } @@ -89,7 +95,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li promise.reject(null,e.getMessage()); } } - + @ReactMethod public void presentLocalNotification(ReadableMap details) { Bundle bundle = Arguments.toBundle(details); @@ -104,11 +110,11 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li @ReactMethod public void cancelLocalNotification(String notificationID) { - mFIRLocalMessagingHelper.cancelLocalNotification(notificationID); + mFIRLocalMessagingHelper.cancelLocalNotification(notificationID); } @ReactMethod public void cancelAllLocalNotifications() { - mFIRLocalMessagingHelper.cancelAllLocalNotifications(); + mFIRLocalMessagingHelper.cancelAllLocalNotifications(); } @ReactMethod @@ -148,13 +154,13 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li @ReactMethod public void getBadgeNumber(Promise promise) { - promise.resolve(mBadgeHelper.getBadgeCount()); + promise.resolve(mBadgeHelper.getBadgeCount()); } private void sendEvent(String eventName, Object params) { getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(eventName, params); } private void registerTokenRefreshHandler() { @@ -174,7 +180,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li public void send(String senderId, ReadableMap payload) throws Exception { FirebaseMessaging fm = FirebaseMessaging.getInstance(); RemoteMessage.Builder message = new RemoteMessage.Builder(senderId + "@gcm.googleapis.com") - .setMessageId(UUID.randomUUID().toString()); + .setMessageId(UUID.randomUUID().toString()); ReadableMapKeySetIterator iterator = payload.keySetIterator(); while (iterator.hasNextKey()) { @@ -208,36 +214,36 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li getReactApplicationContext().registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (getReactApplicationContext().hasActiveCatalystInstance()) { - RemoteMessage message = intent.getParcelableExtra("data"); - WritableMap params = Arguments.createMap(); - WritableMap fcmData = Arguments.createMap(); - - if (message.getNotification() != null) { - Notification notification = message.getNotification(); - fcmData.putString("title", notification.getTitle()); - fcmData.putString("body", notification.getBody()); - fcmData.putString("color", notification.getColor()); - fcmData.putString("icon", notification.getIcon()); - fcmData.putString("tag", notification.getTag()); - fcmData.putString("action", notification.getClickAction()); - } - params.putMap("fcm", fcmData); - params.putString("collapse_key", message.getCollapseKey()); - params.putString("from", message.getFrom()); - params.putString("google.message_id", message.getMessageId()); - params.putDouble("google.sent_time", message.getSentTime()); - - if(message.getData() != null){ - Map data = message.getData(); - Set keysIterator = data.keySet(); - for(String key: keysIterator){ - params.putString(key, data.get(key)); + if (getReactApplicationContext().hasActiveCatalystInstance()) { + RemoteMessage message = intent.getParcelableExtra("data"); + WritableMap params = Arguments.createMap(); + WritableMap fcmData = Arguments.createMap(); + + if (message.getNotification() != null) { + Notification notification = message.getNotification(); + fcmData.putString("title", notification.getTitle()); + fcmData.putString("body", notification.getBody()); + fcmData.putString("color", notification.getColor()); + fcmData.putString("icon", notification.getIcon()); + fcmData.putString("tag", notification.getTag()); + fcmData.putString("action", notification.getClickAction()); } - } - sendEvent("FCMNotificationReceived", params); + params.putMap("fcm", fcmData); + params.putString("collapse_key", message.getCollapseKey()); + params.putString("from", message.getFrom()); + params.putString("google.message_id", message.getMessageId()); + params.putDouble("google.sent_time", message.getSentTime()); + + if(message.getData() != null){ + Map data = message.getData(); + Set keysIterator = data.keySet(); + for(String key: keysIterator){ + params.putString(key, data.get(key)); + } + } + sendEvent("FCMNotificationReceived", params); - } + } } }, intentFilter); } @@ -298,7 +304,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li @Override public void onNewIntent(Intent intent){ // don't call notification if it is started from icon - if(intent.getAction() == "android.intent.action.MAIN"){ + if(intent.getAction().equals("android.intent.action.MAIN")){ return; } sendEvent("FCMNotificationReceived", parseIntent(intent)); diff --git a/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java b/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java index 2c24bab..012bb8f 100644 --- a/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java +++ b/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java @@ -1,7 +1,6 @@ package com.evollu.react.fcm; import android.app.Notification; -import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -17,6 +16,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import java.io.IOException; @@ -62,16 +62,16 @@ public class SendNotificationTask extends AsyncTask { } NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext) - .setContentTitle(title) - .setContentText(bundle.getString("body")) - .setTicker(bundle.getString("ticker")) - .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) - .setAutoCancel(bundle.getBoolean("auto_cancel", true)) - .setNumber(bundle.getInt("number")) - .setSubText(bundle.getString("sub_text")) - .setGroup(bundle.getString("group")) - .setVibrate(new long[]{0, DEFAULT_VIBRATION}) - .setExtras(bundle.getBundle("data")); + .setContentTitle(title) + .setContentText(bundle.getString("body")) + .setTicker(bundle.getString("ticker")) + .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) + .setAutoCancel(bundle.getBoolean("auto_cancel", true)) + .setNumber(bundle.getInt("number")) + .setSubText(bundle.getString("sub_text")) + .setGroup(bundle.getString("group")) + .setVibrate(new long[]{0, DEFAULT_VIBRATION}) + .setExtras(bundle.getBundle("data")); if (bundle.containsKey("ongoing") && bundle.getBoolean("ongoing")) { notification.setOngoing(bundle.getBoolean("ongoing")); @@ -198,10 +198,7 @@ public class SendNotificationTask extends AsyncTask { int notificationID = bundle.containsKey("id") ? bundle.getString("id", "").hashCode() : (int) System.currentTimeMillis(); PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent, - PendingIntent.FLAG_UPDATE_CURRENT); - - NotificationManager notificationManager = - (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + PendingIntent.FLAG_UPDATE_CURRENT); notification.setContentIntent(pendingIntent); @@ -209,9 +206,9 @@ public class SendNotificationTask extends AsyncTask { if (bundle.containsKey("tag")) { String tag = bundle.getString("tag"); - notificationManager.notify(tag, notificationID, info); + NotificationManagerCompat.from(mContext).notify(tag, notificationID, info); } else { - notificationManager.notify(notificationID, info); + NotificationManagerCompat.from(mContext).notify(notificationID, info); } } //clear out one time scheduled notification once fired -- 2.26.2