Commit 35d4bf03 authored by Libin Lu's avatar Libin Lu

requestPermissions for android

parent d14cbc73
......@@ -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<String, String> data = message.getData();
Set<String> 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<String, String> data = message.getData();
Set<String> 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));
......
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<Void, Void, Void> {
}
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<Void, Void, Void> {
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<Void, Void, Void> {
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment