我的通知无效。当我的应用程序打开并收到通知时 - >我点击,然后去活动我需要的东西。但是,当我的应用关闭并获得通知时 - >我点击此通知 - >去家庭活动,而不是我需要的活动。
例如,我有HomeActivity
,NotificationActivity
。当我的应用程序打开并且我收到通知时,我会转到NotificationActivity
但是当我的应用程序关闭后我点击时我会转到HomeActivity
(我不需要去这个地方)。
count++;
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
mBuilder.setVibrate(new long[]{1000, 1000});
mBuilder.setTicker(name);
mBuilder.setContentText(message);
mBuilder.setAutoCancel(true);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
Intent i = new Intent(ctx, NotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
ctx,
count,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager myNotificationManager = (NotificationManager)
ctx.getSystemService(ctx.NOTIFICATION_SERVICE);
myNotificationManager.notify(count, mBuilder.build());
答案 0 :(得分:0)
在您推送通知的服务器端的通知对象中添加click_action 如click_action ='notification_activity' 您的fcm这样发送通知服务器端对象
{
"to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
"collapse_key" : "type_a",
"notification" : {
"body" : "First Notification",
"title": "Collapsing A",
"click_action": "notification_activity"
}
}
在此之后,您必须在制造文件中添加“通知活动”的意图过滤器
<activity android:name=".NotificationActivity"
<intent-filter>
<action android:name="notification_activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
此后,您的Firebase消息传递服务将发生如下变化
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String action = remoteMessage.getNotification().getClickAction();
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(),action);
}
private void sendNotification(String body, String title, String action) {
Intent intent = new Intent(action);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent,PendingIntent.FLAG_ONE_SHOT);
//here your notification builder code...
}
}