gpt4 book ai didi

android - 如何在通知进入android时获取Bundle数据

转载 作者:行者123 更新时间:2023-11-30 05:04:01 28 4
gpt4 key购买 nike

我需要打开特定的 fragment ,当我的 android 应用程序收到通知时,当应用程序被杀死或关闭时,当通知到来并单击通知时,我需要打开 fragment 并调用 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);
}
}

最佳答案

您广播通知。因此您的应用不会在通知托盘上显示通知。

关于android - 如何在通知进入android时获取Bundle数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880511/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com