处理来自 Firebase Android 的通知

Handle notifications from Firebase Android

我尝试了解 Firebase。我成功地收到了它的通知。但是如何在点击通知时打开另一个activity?

这是我的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@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.ic_launcher)
            .setContentTitle("test")
            .setContentText(remoteMessage.getData().get("message"));
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());



}

关于通知的事情,与 firebase 无关。

check this 您需要定义通知的操作

您需要构建一个 PendingIntent 并使用 NotificationManager 进行通知

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.your_icon)
        .setContentTitle("Notification Title")
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

正如 Tosin 所建议的,如果您要从 Firebase 控制台发送推送通知,则需要将有效负载放入 data