【问题标题】:Get Firebase notification when app is in background (with only data-payload using REST client)当应用程序处于后台时获取 Firebase 通知(仅使用 REST 客户端的数据有效负载)
【发布时间】:2017-03-11 01:14:17
【问题描述】:

我想在应用程序仅在后台使用数据有效负载时收到通知。我不想发送任何通知参数,例如"notification" : {"sound": "default"}

这是我接收消息和构建通知的代码。

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String URL_KEY = "url";
private static final String TITLE_KEY = "title";
private static final String BODY_KEY = "body";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getNotification() != null) { //Here I made the dumb mistake
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

//            Notification Data initialization
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//            String notificationTitle = remoteMessage.getNotification().getTitle();
        String notificationTitle = remoteMessage.getData().get(TITLE_KEY);
//            String notificationBody = remoteMessage.getNotification().getBody();
        String notificationBody = remoteMessage.getData().get(BODY_KEY);
        String receivedUrl = remoteMessage.getData().get(URL_KEY);

//            BuildNotificaiton
        Intent internetIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(receivedUrl))
                .setComponent(new ComponentName("com.example.suvajit.webview", "com.example.suvajit.webview.MainActivity"))
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

        Random generator = new Random();
        PendingIntent btn1Intent = PendingIntent.getActivity(this, generator.nextInt(), internetIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(notificationTitle)
                .setContentText(notificationBody)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(defaultSoundUri)
                .setAutoCancel(true)
                .addAction(R.mipmap.ic_launcher, "Previous", btn1Intent) // #0
                .addAction(R.mipmap.ic_launcher, "Pause", null)  // #1
                .addAction(R.mipmap.ic_launcher, "Next", null)    // #2
                .setContentIntent(pendingIntent);

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

这是我的清单:

    <service android:name="com.example.suvajit.webview.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name="com.example.suvajit.webview.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

我正在使用 ARC 发送通知

但是当我只发送数据负载通知时,即使应用程序在前台也不会收到通知

{ "data": {
    "url": "https://www.google.com",
    "body" : "This is your message",
    "title": "v-comply",
}
"to" : "dOa...hXIWnms"
}

当我发送 sound 或其他任何东西作为通知参数并且应用程序在前台时,通知正在正常工作,但是当它在后台时,它 仅显示我的项目名称,我猜数据部分没有收到。

{ "data": {
    "url": "https://www.google.com",
    "body" : "This is your message",
    "title": "v-comply",
},
"notification" : {
    "sound": "default"
},
"to" : "dOa...hXIWnms"
}

下面分别是应用在后台和前台时通知的两张截图。

当我的应用在后台只有数据有效负载时,我怎样才能让它工作?还是我做错了什么?有人请给出解决方案或指导我。

【问题讨论】:

  • 我猜这种问题在堆栈溢出中被问了很多次,但我没有找到我想要的信息。如果可能的话,请指导我找到解决方案。

标签: android firebase notifications firebase-cloud-messaging


【解决方案1】:

这是我犯的一个非常愚蠢和愚蠢的错误。我只是在发送数据有效负载,但我正在检查remoteMessage.getNotification() != null,这意味着如果我的通知数据中没有任何内容,它就不会运行我的代码。条件应该是这样的

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getData().size() > 0) {
    //Do stuffs to show notification with data payload only
    }
}

毕竟我的通知按预期工作。

这是给其他用户的。

如果有人想要分别检查通知和数据负载,他们可以包括这样的两个条件。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getData().size() > 0) {
        //Do stuffs with data payload only
    }

    if (remoteMessage.getNotification() != null) {
        //Do stuffs with notificaiton data
    }
}

【讨论】:

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