应用程序无法显示Firebase通知(广播意图回调:result = CANCELLED forIntent)

3
当我的应用程序被杀死或在后台运行时,我无法收到Firebase通知。
我签名了APK,但它不起作用。当我使用JSON发送数据通知时,logcat会显示以下错误:
   broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.example.paolomazza.firebasedemo3 (has extras) }

以下是 Manifest 文件,导致这个 bug 的可能原因是什么? 谢谢提前。
<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver android:name="com.example.paolomazza.firebasedemo3.OnBootBroadcastReceiver">
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".FirebaseIDService">
        <intent-filter>
            <action android:name="com.example.paolomazza.firebasedemo3.INSTANCE_ID_EVENT"
                android:stopWithTask="false" />
        </intent-filter>
    </service>

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"
                android:stopWithTask="false" />
        </intent-filter>
    </service>
</application>

以下是显示通知的代码:

public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();

    //you can get your text message here.
    String text= data.get("my_custom_key");

    System.out.println(text);
    notifies(text);
}

private void notifies(String body){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(MyFirebaseMessagingService.this)
                    .setSmallIcon(android.R.drawable.ic_menu_help)
                    .setContentTitle("Congratulations!")
                    .setContentText(body);
    Notification mNot= mBuilder.build();
    NotificationManager mNotMan = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    mNotMan.notify(1,mNot);
}

你能否发布你的通知生成代码,其中包括你在哪个活动中创建通知? - Andy Developer
我不是在谈论第一条消息,而是在谈论当您收到Json负载并生成通知时生成通知的代码。您是否实施了上述解决方案并尝试过我发布的内容? - Andy Developer
抱歉,我的意思是我在 Stack Overflow 的主帖中写了我的代码! - Paolo Mazza
问题出在你的通知生成代码中。你没有使用pending intent。请参考此链接:https://developer.android.com/guide/topics/ui/notifiers/notifications.html - Andy Developer
请检查您手机中的任何应用程序是否限制了通知,这种情况现在在许多设备上都会发生。同时,请确保您已允许该应用程序发送通知。为此,请转到应用信息页面,并检查通知部分。 - Andy Developer
显示剩余7条评论
1个回答

3

好的,我刚刚解决了这个问题。

如果您使用的是华为设备或者使用 EMOI 操作系统的设备,只需进入

高级设置

然后进入

电池

受保护的应用

然后选择您的应用程序,将其打开,您就可以在后台接收到通知了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接