Android设备组未收到消息

时间:2019-07-13 11:02:52

标签: android firebase firebase-cloud-messaging

自几个月以来,我没有对应用程序进行任何更改,但是从6月11日开始,我将无法接收Firebase云消息发送的任何消息。已有几位客户报告了它,我可以确认它不再起作用。至少在我的手机上没有固件更改或任何更改。我完全使用 与您发现的here相同的代码。消息已发送,onMessageSent中的回调FirebaseMessagingService被正确调用,但是我无法使用另一部手机上的相同帐户接收任何信息。我想念有什么大变化吗? 我的配置未使用服务器 ,因此我无法检查任何日志服务器端。有提示吗?

3 个答案:

答案 0 :(得分:5)

在收到30封支持Firebase的电子邮件后,令人惊讶的是:我的代码/项目没有任何内容,只是他们不再支持无服务器配置。有趣的是,他们以前不知道为什么以及如何工作!他们太荒谬了。他们没有建议这一重大更改,因此任何基于无服务器的android设备组的应用都无法正常工作。

答案 1 :(得分:1)

以下是您问题的可能答案-

首先,您说“ 但是我无法在另一部手机上使用同一帐户接收任何东西”。通过FCM进行设备消息传递会将设备注册令牌用作联系点,而不是帐户。因此,您可能不会在其他设备上使用同一帐户接收来自FCM的消息。

第二,每个令牌组只能注册20个设备。 Firebase文档还提到您通常需要一台应用服务器来进行设备到设备的消息传递。

请记住,这种设备到设备的消息传递仅用于通知,而不用于发送聊天消息或任何此类消息。如果要在没有应用服务器的情况下实施通知,请尝试以下操作-

import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.legacy.content.WakefulBroadcastReceiver;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class NotificationIntentService extends IntentService {

    private static int NOTIFICATION_ID = 1;
    private static final String ACTION_START = "ACTION_START";
    private static final String ACTION_DELETE = "ACTION_DELETE";

    public NotificationIntentService() {
        super(NotificationIntentService.class.getSimpleName());
    }

    public static Intent createIntentStartNotificationService(Context context) {
        Intent intent = new Intent(context, NotificationIntentService.class);
        intent.setAction(ACTION_START);
        return intent;
    }

    public static Intent createIntentDeleteNotification(Context context) {
        Intent intent = new Intent(context, NotificationIntentService.class);
        intent.setAction(ACTION_DELETE);
        return intent;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.w(String.valueOf(new NotificationIntentService()), "onHandleIntent, started handling a notification event");
        try {
            String action = intent.getAction();
            if (ACTION_START.equals(action)) {
                processStartNotification();
            }
            if (ACTION_DELETE.equals(action)) {
                processDeleteNotification(intent);
            }
        } finally {
            WakefulBroadcastReceiver.completeWakefulIntent(intent);
        }
    }

    private void processDeleteNotification(Intent intent) {
        // Log something?
    }

    private void processStartNotification() {

        // Do something. For example, fetch fresh data from backend to create a rich notification?

        NOTIFICATION_ID += 1;

        Intent intent = new Intent(NotificationIntentService.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(NotificationIntentService.this, 0 /* Request code */, intent,
                                        PendingIntent.FLAG_UPDATE_CURRENT);

         String channelId = getString(R.string.default_notification_channel_id);
         Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         NotificationCompat.Builder notificationBuilder =
                                        new NotificationCompat.Builder(NotificationIntentService.this, channelId)
                                                .setSmallIcon(R.drawable.chat)
                                                .setContentTitle("Title")
                                                .setContentText("Text")
                                                .setAutoCancel(true)
                                                .setSound(defaultSoundUri)
                                                .setContentIntent(pendingIntent);

                                notificationBuilder.setContentIntent(pendingIntent);
                                notificationBuilder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(NotificationIntentService.this));

                                NotificationManager notificationManager =
                                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                                // Since android Oreo notification channel is needed.
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                    NotificationChannel channel = new NotificationChannel(channelId,
                                            "channel_id",
                                            NotificationManager.IMPORTANCE_DEFAULT);
                                    notificationManager.createNotificationChannel(channel);
                                }

                                notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

    }
}
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.legacy.content.WakefulBroadcastReceiver;

import java.util.Calendar;
import java.util.Date;

public class NotificationEventReceiver extends WakefulBroadcastReceiver {

    private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
    private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
    private static final int NOTIFICATIONS_INTERVAL_IN_FIFTEEN_MINUTES = 0;

    public static void setupAlarm(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent alarmIntent = getStartPendingIntent(context);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                getTriggerAt(new Date()),
                600000,
                alarmIntent);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Intent serviceIntent = null;
        if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) {
            Log.w(getClass().getSimpleName(), "onReceive from alarm, starting notification service");
            serviceIntent = NotificationIntentService.createIntentStartNotificationService(context);
        } else if (ACTION_DELETE_NOTIFICATION.equals(action)) {
            Log.w(getClass().getSimpleName(), "onReceive delete notification action, starting notification service to handle delete");
            serviceIntent = NotificationIntentService.createIntentDeleteNotification(context);
        }

        if (serviceIntent != null) {
            startWakefulService(context, serviceIntent);
        }
    }

    private static long getTriggerAt(Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        //calendar.add(Calendar.HOUR, NOTIFICATIONS_INTERVAL_IN_HOURS);
        return calendar.getTimeInMillis();
    }

    private static PendingIntent getStartPendingIntent(Context context) {
        Intent intent = new Intent(context, NotificationEventReceiver.class);
        intent.setAction(ACTION_START_NOTIFICATION_SERVICE);
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static PendingIntent getDeleteIntent(Context context) {
        Intent intent = new Intent(context, NotificationEventReceiver.class);
        intent.setAction(ACTION_DELETE_NOTIFICATION);
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public final class NotificationServiceStarterReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationEventReceiver.setupAlarm(context);
    }
}

对不起,但是如果不查看代码,我将无法告诉您更多信息。

希望这会有所帮助。

来源-

答案 2 :(得分:1)

从GoogleAuthUtil和Plus.API迁移

如果您过去使用GoogleAuthUtil.getToken或Plus.API与Google Sign-In集成,则应迁移到最新的Sign-In API,以提高安全性和更好的用户体验。

Android客户端 如果您要求删除GET_ACCOUNTS(联系人)权限 使用GoogleAuthUtil配置将Plus.APIAccountPicker.newChooseAccountIntent()AccountManager.newChooseAccountIntent()Auth.GOOGLE_SIGN_IN_API的任何代码切换到GoogleSignInOptions.Builder.requestIdToken(...)

https://developers.google.com/identity/sign-in/android/migration-guide

相关问题