我尝试使用Google云消息从我的应用服务器向我的应用发送推送通知消息该应用在Android模拟器上运行良好,但有些设备没有收到通知
这里是FCMMessagingService.java
public class FCMMessagingService extends FirebaseMessagingService {
String title;String message;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null){
title = remoteMessage.getNotification().getTitle();
message = remoteMessage.getNotification().getBody();
Log.d("Message body", remoteMessage.getNotification().getBody().toString());
}
Map<String, String> data = remoteMessage.getData();
title = data.get("company");
message = data.get("message");
Log.d("Message body", remoteMessage.getData().toString());
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
super.onMessageReceived(remoteMessage);
}
}
这里是Manifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".FcmInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
</intent-filter>
</service>
<service
android:name=".FCMMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
答案 0 :(得分:1)
如果您在应用程序位于前台时收到通知,那么您只需使用data payload
代替notification payload
即可同时使用data payload
但data payload
可以使用应用程序在后台甚至关闭。我建议您仅使用payload
,因为它的工作时间是100%。我的应用程序之一在应用程序关闭时没有收到通知但我同时使用了data payload
然后我只将其更改为{{1}}并且工作正常。