如何在android中发出通知时获取Bundle数据

问题描述 投票:1回答:1

当应用程序处于被杀或关闭时,当通知到来时点击通知我需要打开片段并在此处调用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 firebase push-notification firebase-notifications
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.