当通知进入android时如何获取Bundle数据
Posted
技术标签:
【中文标题】当通知进入android时如何获取Bundle数据【英文标题】:How to get Bundle data when notification comes in android 【发布时间】:2019-07-19 16:50:14 【问题描述】:我需要在通知出现在我的 android 应用程序中时打开特定片段,当应用程序在通知到来时被杀死或关闭时,单击通知我需要打开片段并调用 api 这是我的代码。当我的应用程序当时处于运行状态时,通知来了,但没有显示在通知栏上
Bundle bundle = getIntent().getExtras();
if (bundle != null)
String value = bundle.getString("api_url");
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
MyFirebaseMessagingService.class
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
String notification_from = "";
String titles = "", timestamps = "", url = "";
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null)
notification_from = remoteMessage.getData().get("notification_from");
try
Log.e(TAG, "Tejas Data Payload: " + remoteMessage.getData().toString());
JSONObject json = new JSONObject(remoteMessage.getData().toString());
if (json.has("intent"))
JSONObject jsonObject = json.getJSONObject("intent");
if (jsonObject.has("notification_from"))
notification_from = "" + jsonObject.getInt("notification_from");
titles = jsonObject.getString("title");
url = jsonObject.getString("api_url");
timestamps = jsonObject.getString("timestamp");
Log.e(TAG, "Tejas Json: " + json.toString());
catch (Exception e)
e.printStackTrace();
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody(), notification_from, url);
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try
JSONObject json = new JSONObject(remoteMessage.getData().toString());
Log.e("JSON DATA TEJAS: ", json.toString());
handleDataMessage(json);
catch (Exception e)
Log.e(TAG, "Exception: " + e.getMessage());
private void handleNotification(String message, String notification_from, String url)
if (!NotificationUtils.isAppIsInBackground(getApplicationContext()))
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
pushNotification.putExtra("notification_from", notification_from);
pushNotification.putExtra("api_url", url);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
/*
Intent notificationIntent = new Intent(getBaseContext(), ApprovalPendingList.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo_app)
.setContentTitle("SocietyWerx")
.setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
manager.notify(0, builder.build());
*/
if (url.contains("dues_list"))
Intent i_view_post = new Intent(getApplicationContext(), ApprovalPendingList.class);
notificationUtils.showNotificationMessage(getString(R.string.app_name), message, Functions.getCurrentTime(), i_view_post);
/*
Intent i_view_post = new Intent(getApplicationContext(), FragmentDues.class);
notificationUtils.showNotificationMessage(getString(R.string.app_name), message, Functions.getCurrentTime(), i_view_post);
*/ //startActivity(i_view_post);
else if (url.contains(""))
Intent i_view_post = new Intent(getApplicationContext(), ApprovalPendingList.class);
notificationUtils.showNotificationMessage(getString(R.string.app_name), message, Functions.getCurrentTime(), i_view_post);
else
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
pushNotification.putExtra("notification_from", notification_from);
pushNotification.putExtra("api_url", url);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
private void handleDataMessage(JSONObject json)
Log.e(TAG, "push json: " + json.toString());
try
JSONObject data = json.getJSONObject("data");
String body = data.getString("body");
String title = data.getString("title");
String message = data.getString("message");
String notification_from = "" + data.getInt("notification_from");
boolean isBackground = data.getBoolean("is_background");
String imageUrl = data.getString("image");
String timestamp = data.getString("timestamp");
JSONObject payload = data.getJSONObject("payload");
String url = data.getString("api_url");
handleNotification(body, notification_from, url);
Log.e(TAG, "title: " + title);
Log.e(TAG, "message: " + message);
Log.e(TAG, "isBackground: " + isBackground);
Log.e(TAG, "payload: " + payload.toString());
Log.e(TAG, "imageUrl: " + imageUrl);
Log.e(TAG, "timestamp: " + timestamp);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext()))
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
pushNotification.putExtra("notification_from", notification_from);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
if (TextUtils.isEmpty(imageUrl))
showNotificationMessage(getApplicationContext(), title, message, timestamp, pushNotification);
else
// image is present, show notification with image
showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, pushNotification, imageUrl);
else
// app is in background, show the notification in notification tray
Intent resultIntent = new Intent(getApplicationContext(), ApprovalPendingList.class);
resultIntent.putExtra("message", message);
resultIntent.putExtra("notification_from", notification_from);
// check for image attachment
if (TextUtils.isEmpty(imageUrl))
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
else
// image is present, show notification with image
showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
catch (JSONException e)
Log.e(TAG, "Json Exception: " + e.getMessage());
catch (Exception e)
Log.e(TAG, "Exception: " + e.getMessage());
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent)
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
/**
* Showing notification with text and image
*/
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl)
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
【问题讨论】:
您只需要从 FCM 获取 data 类型的通知,即使在应用程序被终止时也能接收通知。 你好,你能把你的通知json数据粘贴到这里吗? 【参考方案1】:您广播了通知。因此,您的应用不会在通知托盘上显示通知。
【讨论】:
以上是关于当通知进入android时如何获取Bundle数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ios 中单击通知时接收数据,当应用程序被杀死时,(react-native-fcm 模块在 IOS 中获取推送通知)
如果我的 Android APK Bundle 是 45 MB,那么当它进入 Google Play 商店时,这就是我的应用程序的大小吗?