应用在后台时获取Firebase通知(仅使用REST客户端使用数据负载)

时间:2016-10-28 07:23:41

标签: android firebase notifications firebase-cloud-messaging

我想在应用程序处于后台时仅接收数据有效负载时收到通知。我不想发送任何通知参数,例如"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发送通知

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"
}

下面分别有两个app在后台和前台的通知截图。

When App in Background When app in foreground

如果我的应用在后台只有数据负载时,如何让它能够收到通知?或者我做错了什么?有人请给出解决方案或指导我。

1 个答案:

答案 0 :(得分:0)

这是我犯下的一个非常愚蠢和愚蠢的错误。我只发送数据有效负载,但我正在检查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
    }
}