【问题标题】:Handle notifications from Firebase Android处理来自 Firebase Android 的通知
【发布时间】:2016-10-04 14:54:59
【问题描述】:

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

这是我的代码:

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



}

【问题讨论】:

标签: android firebase android-notifications firebase-cloud-messaging firebase-notifications


【解决方案1】:

您需要构建一个 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

【讨论】:

    【解决方案2】:

    这是关于通知的,与firebase无关。

    check this你需要定义通知的动作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多