Мне нужно открыть определенный фрагмент, когда уведомление приходит в моем приложении для 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);
}
}