当应用处于前台和后台时,我收到通知。但是当我关闭应用程序时,没有收到任何通知。
这是我的宣言
<service android:name="com.vfi.fcm.AppInstanceIDListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name="com.vfi.fcm.AppFcmListenerService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
这是FCM侦听器类
public class AppFcmListenerService extends FirebaseMessagingService {
private static final String TAG = "FCM - Msg";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> data = remoteMessage.getData();
Set keys = data.keySet();
for (Iterator i = keys.iterator(); i.hasNext(); ) {
String key = (String) i.next();
Log.e("key",key);
String value = (String) data.get(key);
sendNotification(null, value);
Log.e("value",value);
}
}
private void sendNotification(String type, String message) {
Random random = new Random();
int Unique_Integer_Number = random.nextInt(9999 - 1000) + 1000;
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), Unique_Integer_Number, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder.setContentTitle(AppControl.getInstance().getAppName());
builder.setContentText(message);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setSound(defaultSoundUri);
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
nManager.notify(AppControl.getInstance().getAppName(), Unique_Integer_Number, builder.build());
}
我在Leeco 2设备上测试它。应用程序关闭时不会收到通知。
我已尝试过Firebase background 提供的解决方案 但它都没有解决我的问题。
这是我的有效载荷
{
"data": {
"Message": "great match!"
},
"to": "fcm-id"
}