android - Firebase 云消息传递 : not receiving message in android 12

标签 android firebase firebase-cloud-messaging

我正在尝试使用 Firebase 消息服务向 android 12 设备 发送推送通知。当我尝试从 nodeJs 发送 FCM 时,onMessageReceived 未被调用。我尝试从 Firebase 控制台发送 FCM,但即使该应用程序在后台,该应用程序也没有显示任何通知或错误。我已经在 android 11android 10 中测试了这个应用,两者都运行良好。

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />


<application
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MobileSMSService"
        android:usesCleartextTraffic="true">

<service
        android:name=".helper.MessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/sms_icon_foreground" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/purple_700" />
</service>
</application>

消息服务.java

public class MessagingService extends FirebaseMessagingService {

    private static final String CHANNEL_ID = "FCM";
    private static final String TAG = "MessagingService";
    whatsappMessageService messageService;

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        JSONObject obj = new JSONObject();
        SharedPreferences sp = getSharedPreferences("Login", MODE_PRIVATE);
        String api = sp.getString("api", null);
        sp.edit().putString("token", s).apply();
        try {
            obj.put("token", s);
            obj.put("api", api);
            new Server().post(new Server().baseUrl + "update-token", obj.toString(), new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {

                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                }
            });
        } catch (Exception e) {
            Log.e("MessagingService", "onNewToken: ", e);
        }
    }

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

        Log.e(TAG, "onMessageReceived: recived" );
        Map<String, String> data = remoteMessage.getData();
        if (data.get("web") == null) {
            String message = data.get("message"), to = data.get("to"), plat = data.get("plat");
            if (plat != null) {
                if (plat.equals("w")) {
                    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                    boolean isScreenOn = pm.isScreenOn();
                    boolean isScreenOnApi21 = pm.isInteractive();
                    if (!isScreenOn | !isScreenOnApi21) {
                        SharedPreferences sp = getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
                        if (sp.getString("wake_phone", null) != null) {
                            sendSMS(sp.getString("wake_phone", null), "Wake up");
                            new MessagesDatabaseHelper(this).insertMessage(sp.getString("wake_phone", null), "Wake up");
                            try {
                                Thread.sleep(3500);
                                isScreenOn = pm.isScreenOn();
                                isScreenOnApi21 = pm.isInteractive();
                                if (!isScreenOn | !isScreenOnApi21) {
                                    Thread.sleep(3500);
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            return;
                        }
                    }

                    if (messageService == null) {
                        messageService = new whatsappMessageService(getApplicationContext());
                        messageService.start();
                    }
                    messageService.addMessage(to, message);
                    new WhatsappDatabaseHelper(this).insertMessage(to, message);
                } else {
                    new MessagesDatabaseHelper(this).insertMessage(to, message);
                    sendSMS(to, message);
                }

                Intent intents = new Intent();
                intents.setAction("sms_receiver");
                getBaseContext().sendBroadcast(intents);
            }
        } else {
            Log.e(TAG, "onMessageReceived: " + "web");
            new webManager().onMessage(data);
        }

    }

最佳答案

对于可能遇到这个问题的新人,我遇到了完全相同的问题:
就我而言,我仍在使用具有旧的已弃用架构的 Firebase 库 17.x。
现在我将 firebase 依赖更新到版本 23.0.4,它工作得很好。

关于android - Firebase 云消息传递 : not receiving message in android 12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71953744/

相关文章:

android - CrashlyticsInitProvider 跳过初始化

ios - Testflight 中 iOS 的 Firebase 通知问题

firebase - 如何通过 API 获取 Firebase 的所有主题列表?

java - 我的应用程序在启动之前崩溃了。这是一个简单的应用程序,我在其中使用 Intent 来调用前一个类中的另一个类

javascript - 电话间隙 : uploading file to server using multipart/form-data

firebase - 如何使用结构表示Cloud Firestore中的分层数据?

ios - APNs 证书更改后,FCM 不发送推送通知

android - 我可以在非 root 设备上使用 sqlite 3

java通过telnet发送固件

firebase - 依赖文档引用的Firestore访问规则