我正在使用react-native推送通知模块。当我通过谷歌FCM发出通知时,我想在应用程序被杀或后台时在存储中添加键值值。我写了代码,但它没有用。我尝试使用AsyncStorage
和react-native shared-preferences module获取密钥。但总是变空。
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);
});
使用AsyncStorage
await AsyncStorage.getItem('key');
答案 0 :(得分:0)
如果您通过FCM面板发送推送通知,则如果您的应用处于后台,则不会调用onReceive。您需要发送&#34;数据消息&#34;。这是唯一可以从中创建自定义通知或调用自定义意图的消息类型。如果您从FCM控制台发出标准通知,则操作系统将自动创建通知,并为其创建意图。您可以阅读更多相关信息here。