我正在使用FCM进行推送通知,我只处理数据有效负载。当应用在前景时,多个通知正常工作,在这里,我在click_action中使用iD值在我们点击时将通知移至相关帖子。我将点击一份通知,它将转到相关帖子,但剩下的所有内容并没有将相关帖子重定向,只是从通知中清除。我没有发现为什么会发生这个问题。在这里我的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
}
private void sendNotification(String title, String message, String id, String image) {
Log.d("notificationdetails",title+",,,"+message+",,"+id);
Intent intent = null;
int postid = Integer.valueOf(id);
if(id.equals("")|| id==null) {
intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else{
Constants.PushId = postid;
intent = new Intent(this, DetailActivity.class);
Log.d("mypostid",postid+"");
intent.putExtra("id", postid);
intent.putExtra("backpage","main");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, postid, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_primepost)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int num = random.nextInt(99999-1000)+1000;
notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
}
}
sustest.xml
<activity android:name="com.primepostnews.app.Activities.DetailActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="Detail" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
有两种类型的消息数据消息和通知消息。数据消息在此处在此处处理,无论该应用在前景还是背景中。数据消息是GCM传统上使用的类型。仅当应用程序在前景时,仅在此处收到通知消息。当该应用在后台时,将显示自动生成的通知。包含通知和数据有效载荷的消息被视为通知消息。Firebase控制台总是发送通知消息。
if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
} else if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getBody(),remoteMessage.getData().get("click_action"),image);
}
您应该使用通知类型消息在所有情况下使用。当应用在后台杀死应用时,数据类型不起作用。因为对于数据类型通知,请拨打ONMESSAGERECERENE,您必须使用NotificationBuilder手动创建状态栏通知。
所以最好使用服务器的通知类型输入,例如以下
"notification": {
"title": "Firebase notification",
"message": "I am firebase notification. you can customise me. enjoy",
"click_action": "OPEN_ACTIVITY",
"sound":"default",
}
在所有情况下,这都可以正常工作,您不想担心在代码上处理它。