【问题标题】:open specific activity when e push notifications from firebase当 e 从 firebase 推送通知时打开特定活动
【发布时间】:2017-06-14 12:10:17
【问题描述】:

我有 android 应用程序并尝试接收推送通知。 单击保留通知打开主要活动时我的问题, 当收到来自 firebase 的推送通知时,我需要直接打开特定活动。这是我的代码

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);
        }
    }

【问题讨论】:

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

您可以在通知中设置待处理的意图。所以首先创建一个待处理的意图:

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

然后添加到你的NotificationBuilder,在这里你定义了通知的标题、描述等,下面的参数:

builder.setContentIntent(contentIntent)

更新:

您的代码中已经有您的NotificationCompatBuilder

替换这个:

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

用这个:

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);

【讨论】:

  • 在哪里??添加这个?!!
  • @MoayedAlayseh 哎呀……一个拼写错误。我修好了它。可以再试一次吗?
  • @MoayedAlayseh 你设置了目的地活动吗?
  • PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Notification_Page.class), 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.app_logo_a) .setContentTitle(remoteMessage.getData().get("title")) .setContentText(remoteMessage.getNotification().getBody()) .setContentIntent(contentIntent); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(0, builder.build());
  • 这是我的代码,当点击通知应用程序打开第一个活动!!!
【解决方案2】:

您应该考虑使用PendingIntent 以便能够在单击通知时启动特定活动。将此添加到您的代码中

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

即使您没有很好地实现handleDataMessage,您也可以将代码更改为此,但这将允许您在单击通知时启动活动

@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();

    }
}

【讨论】:

  • 在哪里??添加这个?!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2017-09-15
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
相关资源
最近更新 更多