当应用被杀或在后台时,我无法收到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);
}
答案 0 :(得分:2)
好的,我刚解决了这个问题。
如果您使用的是华为设备或使用EMOI操作系统的设备,请转到
高级设置
而不是去
电池
和
受保护的应用
选择您的应用,打开它,您就可以在后台接收通知了!