【问题标题】:Intent of Notification does not follow my url通知意图不符合我的网址
【发布时间】:2018-05-11 04:22:37
【问题描述】:

我有一个使用 Firebase 发送通知的应用。当我发送它们并显示通知时,提供了样式,但通知的其余部分与描述不符

Notification notification = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.lascanadaslogo)
            .setContentTitle("Titulo")
            .setContentIntent(pi)
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
            .build();

此外,当我单击它时,它会执行一个意图,但不是对我的 url 执行此操作,而是对我的应用程序 (MainPage) 执行此操作。我给你看完整的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService{

    private static final String TAG = "FCM Service";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.lascanadas.es/reservas"));
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.lascanadaslogo)
                .setContentTitle("Titulo")
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
                .build();

        NotificationManager notificationManager2 =  (NotificationManager) getApplicationContext().getSystemService(Service.NOTIFICATION_SERVICE);
        notificationManager2.notify(0, notification);

        //Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

【问题讨论】:

    标签: android android-studio firebase android-intent notifications


    【解决方案1】:

    如果您使用Firebase 控制台通知通知,那么如果您的用户没有使用您的应用程序(应用程序在后台),那么您无法控制通知,这一切都将由 Firebase SDK 完成,点击通知将打开Launcher Activity (MainActivity)。但是,如果您的用户正在使用您的应用程序(在前台),那么您的 onMessageReceived() 方法将被调用,因此您的逻辑将起作用。但它总是在您的用户不使用您的应用程序时发生,因此我建议您可能使用服务器使用数据消息。使用服务器更容易。从那里您的onMessageReceived 将始终被调用,您可以简单地检索数据:

    Map<String,String> your_data = remoteMessage.getData;
    

    【讨论】:

    • 非常感谢!我有一个覆盆子作为服务器。我必须使用哪个程序?
    • @Jorge。我知道肯定有一个简单的替代方案。我会帮你的。 johnnnoni@gmail.com。不鼓励在 cmets 中进行长时间的讨论。那就用我的邮箱吧。
    【解决方案2】:

    在后台向应用发送通知消息时,不会调用onMessageReceived();通知由 SDK 生成。这在the documentation中有解释:

    当您的应用处于后台时发送的通知消息。在 在这种情况下,通知会发送到设备的系统托盘。 默认情况下,用户点击通知会打开应用启动器。

    如果您希望始终调用onMessageReceived() 以便控制通知生成,则需要发送data message instead of a notification message

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 2013-08-30
      • 2014-05-02
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多