open specific activity when e push notifications from firebase

巧了我就是萌 提交于 2021-01-28 07:07:51

问题


I have android application and tried to receive a push notifications. My problem when click the reserved notification open the main activity, I need when received push notifications from firebase open a specific activity directly. This is my code

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody());
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e("Mo", "Exception: " + e.getMessage());
        }
        Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
        startActivity(resultIntent);
    }

    private void handleDataMessage(JSONObject json) {

        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();

            Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
            startActivity(resultIntent);
        } else {
            Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
            startActivity(resultIntent);
        }
    }

回答1:


You can set a pending intent in to your notification. So first create a pending intent:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), 0);

Then add to your NotificationBuilder, where you define the title, description, etc.. of the notification, the following parameter:

builder.setContentIntent(contentIntent)

UPDATE:

You have your NotificationCompatBuilder already in your code.

Replace this:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody());

with this:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody())
                .setContentIntent(contentIntent);



回答2:


You should consider using PendingIntent to be able to start a specific activity when clicking on the notification. Add this to your code

    Intent intent = new Intent(this, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, YOUT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

You can change your code to this even though you are not implmented the handleDataMessage well but this will allow you to start an activity when clicking on the notification

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));

    //two lines addded
    Intent intent = new Intent(this, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.app_logo_a)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentIntent(contentIntent);//added line
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
    try {
        JSONObject json = new JSONObject(remoteMessage.getData().toString());
        handleDataMessage(json);
    } catch (Exception e) {
        Log.e("Mo", "Exception: " + e.getMessage());
    }

}

private void handleDataMessage(JSONObject json) {

    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
        // app is in foreground, broadcast the push message
        Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
        pushNotification.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
        NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();

    }
}


来源:https://stackoverflow.com/questions/44544422/open-specific-activity-when-e-push-notifications-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!