某些设备无法接收FCM通知

时间:2016-06-08 21:25:11

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

我遇到了问题,因为我正在测试我的应用的某些设备没有收到通知,也没有引发异常。

通知来自FCM,我正在使用自定义服务来显示它们。

MyFirebaseMessaginService.java

static int count = 0;
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {

    Log.i("remoteMessage",remoteMessage.toString());
    switch(remoteMessage.getData().get("tipo")){
        case "normal":
            notificacionNormal(remoteMessage);
            break;
        case "imagen":
            notificacionImagen(remoteMessage);
            break;
        case "imagen+url":
            notificacionImagenUrl(remoteMessage);
            break;
    }
    count++;
    //Log.d("prueba",remoteMessage.getData().get("imagen"));
}

private void notificacionImagenUrl(RemoteMessage remoteMessage) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(remoteMessage.getData().get("url")));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, i, PendingIntent.FLAG_ONE_SHOT);

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notif = new Notification.Builder(this)
            .setContentIntent(pendingIntent)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setStyle(new Notification.BigPictureStyle().bigPicture(getImagae(remoteMessage.getData().get("imagen"))))
            .build();
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(count, notif);

}

目前它只发生在带有android 6.0.1的root设备中 希望你能帮助我:)。

编辑: 我发送的通知HTTP请求:

"to": "/topics/general",
        "notification" : {
          "title": "title",
          "body": "body",
        },
        "data": {
          "tipo": "normal",
        }

3 个答案:

答案 0 :(得分:1)

可能的原因之一可能是您正在测试的某些设备没有Google Play服务或其更新版本。在您的活动中,请尝试检查Google Play服务。您可以将此功能用作已解答的here

protected boolean checkPlayServices() {
final int resultCode = 

GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity());
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity(),
                    PLAY_SERVICES_RESOLUTION_REQUEST);
            if (dialog != null) {
                dialog.show();
                dialog.setOnDismissListener(new OnDismissListener() {
                    public void onDismiss(DialogInterface dialog) {
                        if (ConnectionResult.SERVICE_INVALID == resultCode) activity().finish();
                    }
                });
                return false;
            }
        }
        new CSAlertDialog(this).show("Google Play Services Error",
                "This device is not supported for required Goole Play Services", "OK", new Call() {
                    public void onCall(Object value) {
                        activity().finish();
                    }
                });
        return false;
    }
    return true;
}

答案 1 :(得分:0)

有时在低端设备上会发生这种情况。一种解决方案是清除Google Play服务应用的应用数据和缓存。这对我有用。

答案 2 :(得分:0)

我不知道这是否有效,但你的原始JSON正文是错误的。您忘记了{ "to": "/topics/general", "notification" : { "title": "title", "body": "body", }, "data": { "tipo": "normal", } } 外部作为JSON对象。以下应该是您要发送的消息。

<view> to <View>