2016-09-29 5 views
0

Я использую интерактивный модуль push-уведомлений. Когда я запускаю уведомление через google FCM, я хочу добавить патч ключевого значения в хранилище, когда приложение будет убито или в фоновом режиме. Я написал код, но он не работает. Я попытался получить ключ, используя AsyncStorage и react-native shared-preferences module. Но всегда получает нуль.onReceive метод не работает в android

Java код

package com.dieam.reactnativepushnotification.modules; 

import android.app.Activity; 
import android.app.Application; 
import android.app.NotificationManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Build; 
import android.os.Bundle; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.util.Log; 

import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper; 
import com.facebook.react.bridge.ActivityEventListener; 
import com.facebook.react.bridge.Arguments; 
import com.facebook.react.bridge.Promise; 
import com.facebook.react.bridge.ReactApplicationContext; 
import com.facebook.react.bridge.ReactContext; 
import com.facebook.react.bridge.ReactContextBaseJavaModule; 
import com.facebook.react.bridge.ReactMethod; 
import com.facebook.react.bridge.ReadableArray; 
import com.facebook.react.bridge.ReadableMap; 
import com.facebook.react.bridge.WritableMap; 
import com.facebook.react.modules.core.DeviceEventManagerModule; 


import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.HashMap; 
import java.util.Map; 
import java.util.Random; 
import java.util.Set; 

public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener { 
    public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag 

    private RNPushNotificationHelper mRNPushNotificationHelper; 
    private final Random mRandomNumberGenerator = new Random(System.currentTimeMillis()); 

    public RNPushNotification(ReactApplicationContext reactContext) { 
     super(reactContext); 

     reactContext.addActivityEventListener(this); 

     Application applicationContext = (Application) reactContext.getApplicationContext(); 
     mRNPushNotificationHelper = new RNPushNotificationHelper(applicationContext); 

     registerNotificationsRegistration(); 
     registerNotificationsReceiveNotification(); 
     registerNotificationsRemoteFetch(); 
    } 

    @Override 
    public String getName() { 
     return "RNPushNotification"; 
    } 

    @Override 
    public Map<String, Object> getConstants() { 
     final Map<String, Object> constants = new HashMap<>(); 

     return constants; 
    } 

    private void sendEvent(String eventName, Object params) { 
     ReactContext reactContext = getReactApplicationContext(); 

     if (reactContext.hasActiveCatalystInstance()) { 
      reactContext 
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) 
        .emit(eventName, params); 
     } 
    } 

    public void onNewIntent(Intent intent) { 
     if (intent.hasExtra("notification")) { 
      Bundle bundle = intent.getBundleExtra("notification"); 
      bundle.putBoolean("foreground", false); 
      intent.putExtra("notification", bundle); 
      notifyNotification(bundle); 
     } 
    } 

    private void registerNotificationsRegistration() { 
     IntentFilter intentFilter = new IntentFilter(getReactApplicationContext().getPackageName() + ".RNPushNotificationRegisteredToken"); 

     getReactApplicationContext().registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String token = intent.getStringExtra("token"); 
       WritableMap params = Arguments.createMap(); 
       params.putString("deviceToken", token); 
       SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
       SharedPreferences.Editor editor = sharedPref.edit(); 
       editor.putInt("key", 100); editor.commit(); 
       sendEvent("remoteNotificationsRegistered", params); 
       Log.e("Notification","registerNotificationsRegistration"); 
      } 
     }, intentFilter); 
    } 

    private void registerNotificationsReceiveNotification() { 
     IntentFilter intentFilter = new IntentFilter(getReactApplicationContext().getPackageName() + ".RNPushNotificationReceiveNotification"); 
     getReactApplicationContext().registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       notifyNotification(intent.getBundleExtra("notification")); 
       Log.e("Notification","registerNotificationsReceiveNotification"); 
       SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
       SharedPreferences.Editor editor = sharedPref.edit(); 
       editor.putInt("key", 99); editor.commit(); 
      } 
     }, intentFilter); 
    } 

    private void registerNotificationsRemoteFetch() { 
     IntentFilter intentFilter = new IntentFilter(getReactApplicationContext().getPackageName() + ".RNPushNotificationRemoteFetch"); 
     getReactApplicationContext().registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Bundle bundle = intent.getBundleExtra("notification"); 
       String bundleString = convertJSON(bundle); 
       WritableMap params = Arguments.createMap(); 
       params.putString("dataJSON", bundleString); 
       SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
       SharedPreferences.Editor editor = sharedPref.edit(); 
       editor.putInt("key", 98); editor.commit(); 
       sendEvent("remoteFetch", params); 
       Log.e("Notification","registerNotificationsRemoteFetch"); 
      } 
     }, intentFilter); 
    } 

    private void notifyNotification(Bundle bundle) { 
     String bundleString = convertJSON(bundle); 

     WritableMap params = Arguments.createMap(); 
     params.putString("dataJSON", bundleString); 
     sendEvent("remoteNotificationReceived", params); 
     Log.e("Notification","notifyNotification"); 
    } 

    private void registerNotificationsReceiveNotificationActions(ReadableArray actions) { 
     IntentFilter intentFilter = new IntentFilter(); 
     // Add filter for each actions. 
     for (int i = 0; i < actions.size(); i++) { 
      String action = actions.getString(i); 
      intentFilter.addAction(getReactApplicationContext().getPackageName() + "." + action); 
     } 
     getReactApplicationContext().registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Bundle bundle = intent.getBundleExtra("notification"); 

       // Notify the action. 
       notifyNotificationAction(bundle); 

       // Dismiss the notification popup. 
       NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
       int notificationID = Integer.parseInt(bundle.getString("id")); 
       manager.cancel(notificationID); 
       SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
       SharedPreferences.Editor editor = sharedPref.edit(); 
       editor.putInt("key", 96); editor.commit(); 
       Log.e("Notification","registerNotificationsRemoteFetch"); 

      } 
     }, intentFilter); 
    } 

    private void notifyNotificationAction(Bundle bundle) { 
     String bundleString = convertJSON(bundle); 

     WritableMap params = Arguments.createMap(); 
     params.putString("dataJSON", bundleString); 

     sendEvent("notificationActionReceived", params); 
    } 

    private String convertJSON(Bundle bundle) { 
     JSONObject json = new JSONObject(); 
     Set<String> keys = bundle.keySet(); 
     for (String key : keys) { 
      try { 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
        json.put(key, JSONObject.wrap(bundle.get(key))); 
       } else { 
        json.put(key, bundle.get(key)); 
       } 
      } catch (JSONException e) { 
       return null; 
      } 
     } 
     return json.toString(); 
    } 

    @ReactMethod 
    public void requestPermissions(String senderID) { 
     ReactContext reactContext = getReactApplicationContext(); 

     Intent GCMService = new Intent(reactContext, RNPushNotificationRegistrationService.class); 

     GCMService.putExtra("senderID", senderID); 
     reactContext.startService(GCMService); 
    } 

    @ReactMethod 
    public void presentLocalNotification(ReadableMap details) { 
     Bundle bundle = Arguments.toBundle(details); 
     // If notification ID is not provided by the user, generate one at random 
     if (bundle.getString("id") == null) { 
      bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt())); 
     } 
     mRNPushNotificationHelper.sendNotification(bundle); 
    } 

    @ReactMethod 
    public void scheduleLocalNotification(ReadableMap details) { 
     Bundle bundle = Arguments.toBundle(details); 
     // If notification ID is not provided by the user, generate one at random 
     if (bundle.getString("id") == null) { 
      bundle.putString("id", String.valueOf(mRandomNumberGenerator.nextInt())); 
     } 
     mRNPushNotificationHelper.sendNotificationScheduled(bundle); 
    } 

    @ReactMethod 
    public void getInitialNotification(Promise promise) { 
     WritableMap params = Arguments.createMap(); 
     Activity activity = getCurrentActivity(); 
     if (activity != null) { 
      Intent intent = activity.getIntent(); 
      Bundle bundle = intent.getBundleExtra("notification"); 
      if (bundle != null) { 
       bundle.putBoolean("foreground", false); 
       String bundleString = convertJSON(bundle); 
       params.putString("dataJSON", bundleString); 
      } 
     } 
     promise.resolve(params); 
    } 

    @ReactMethod 
    public void setApplicationIconBadgeNumber(int number) { 
     ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(getReactApplicationContext(), number); 
    } 

    // removed @Override temporarily just to get it working on different versions of RN 
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { 
     onActivityResult(requestCode, resultCode, data); 
    } 

    // removed @Override temporarily just to get it working on different versions of RN 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // Ignored, required to implement ActivityEventListener for RN 0.33 
    } 

    @ReactMethod 
    /** 
    * Cancels all scheduled local notifications, and removes all entries from the notification 
    * centre. 
    * 
    * We're attempting to keep feature parity with the RN iOS implementation in 
    * <a href="https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/RCTPushNotificationManager.m#L289">RCTPushNotificationManager</a>. 
    * 
    * @see <a href="https://facebook.github.io/react-native/docs/pushnotificationios.html">RN docs</a> 
    */ 
    public void cancelAllLocalNotifications() { 
     mRNPushNotificationHelper.cancelAllScheduledNotifications(); 
     mRNPushNotificationHelper.clearNotifications(); 
    } 

    @ReactMethod 
    /** 
    * Cancel scheduled notifications, and removes notifications from the notification centre. 
    * 
    * Note - as we are trying to achieve feature parity with iOS, this method cannot be used 
    * to remove specific alerts from the notification centre. 
    * 
    * @see <a href="https://facebook.github.io/react-native/docs/pushnotificationios.html">RN docs</a> 
    */ 
    public void cancelLocalNotifications(ReadableMap userInfo) { 
     mRNPushNotificationHelper.cancelScheduledNotification(userInfo); 
    } 

    @ReactMethod 
    public void registerNotificationActions(ReadableArray actions) { 
     registerNotificationsReceiveNotificationActions(actions); 
    } 
} 

Javascript

модулем привилегированную акцию

SharedPreferences.getItem("key", function(value){ 
    console.log(value); 
    }); 

По usingAsyncStorage

await AsyncStorage.getItem('key'); 

ответ

0

Если вы отправляете push-уведомления через панель FCM, то не будет вызываться onReceive, если ваше приложение находится в фоновом режиме. Вам необходимо отправить «сообщение данных». Это единственный тип сообщения, из которого вы можете создавать настраиваемое уведомление, или вызывать пользовательские намерения. Если вы используете стандартное уведомление из консоли FCM, то ОС автоматически создаст уведомление и создаст для него намерение. Вы можете узнать больше об этом here.

Смежные вопросы