当应用程序不在前台时,特定的活动不会从通知点击中打开(应用程序在最近,而不是被杀死!)



我想在点击通知时打开一个特定的活动。但当应用程序处于后台时,它不会打开。我甚至没有传递额外的数据。我只想打开活动,根据登录的用户,我做一些任务。我甚至尝试在通知点击时打开我的默认启动器活动,并从那里将用户发送到通知活动。以下是我的默认启动器活动的代码:附言:我从firebase控制台发送消息,但它只有标题和正文。

(这是我在完成网络任务后调用的功能:(

if (list.size()!=0){
for (int i=0;i<list.size();i++){
Users u=list.get(i);
//Toast.makeText(LoginActivity.this, "Logging in...", Toast.LENGTH_SHORT).show();
final Intent intent;
Bundle b=new Bundle();
// This is the solution i found to check whether the extras has the package name or not! But it doesnt seem to work.
if (Splashscreen.this.getIntent().getExtras() != null){
if (Splashscreen.this.getIntent().hasExtra("pushnotification") || Splashscreen.this.getIntent().getExtras().containsKey("com.tracecost")){
System.out.println("From notification----------->");
intent=new Intent(Splashscreen.this,NotificationReceivedActivity.class);
b.putString("pushnotification","yes");
}
else{
intent=new Intent(Splashscreen.this, ProjectSelection.class);
}
}
else{
intent=new Intent(Splashscreen.this, ProjectSelection.class);
}
b.putSerializable("user",u);
b.putSerializable("projectlist",plist);
intent.putExtras(b);
//logginDialog.dismiss();
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(intent);
finish();
}
},3500);
}
}
}```

当应用程序处于前台或后台时,您可以从Firebase通知重定向到您在data中发送的位置。

我将为您提供一个示例,说明如何根据从通知中收到的密钥在活动之间重定向,firebase神奇地处理其余部分

p.S:如果要处理已在后台打开的活动,请使用taskAffinity进行处理。

通过FirebaseMessagingService:处理Firebase数据

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
System.out.println("MESSAGE BODY :" + remoteMessage.toString());
if (remoteMessage.getData().size() > 0) {
//getting the title and the body
String redirect;
String title = remoteMessage.getData().get("message_title");
String body = remoteMessage.getData().get("message_body");
redirect = remoteMessage.getData().get("redirect");
String event_id = remoteMessage.getData().get("event_id");
System.out.println("PUSH MESSAGE = " + remoteMessage.toString());
JSONObject jsonData = new JSONObject(remoteMessage.getData());
System.out.println("RESPONSE :" + jsonData.toString());
if (redirect != null) {
sendNotification(title, body, event_id, redirect);
} else {
redirect = "";
sendNotification(title, body, event_id, redirect);
}
}
}

private void sendNotification(String title, String body, String event_id, String redirect) {
Intent backIntent = new Intent();
PendingIntent pendingIntent;
if (redirect.contentEquals("CHAT")) {
backIntent = new Intent(MyFirebaseMessagingService.this, ChatScreen.class);
backIntent.putExtra("item_id", event_id);
}
if (redirect.contentEquals("EVENT") || redirect.contentEquals("INVITATION")) {
backIntent = new Intent(MyFirebaseMessagingService.this, Event_Summary.class);
backIntent.putExtra("event_id", event_id);
}
backIntent.putExtra("MODE", "FIRE_NOTIFICATION");
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(backIntent);
pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

这只适用于应用程序处于前台的情况。当应用程序处于后台时,它只考虑来自有效负载的通知数据,而忽略数据部分。这导致无法控制应用程序,因为通知类型的消息将由系统单独处理。唯一的选择是使用自己的外部服务器或从rest客户端发送。

Firebase控制台仅发送通知消息(此处为Firebase的更多信息(,但您可以使用自己的服务器或Firebase API发送

Data消息数据消息适用于前台和后台应用程序状态

触发推送通知的样本卷曲看起来像:

curl-X POST\https://fcm.googleapis.com/fcm/send\-H'授权:密钥=YOUR_API_key'\-H'内容类型:application/json'\-H‘缓存控制:无缓存’\-d'{"数据":{"title":"title","消息":"通知内容","custom_key":"custom_value"},"registration_ids":["DEVICE_PUSH_TOKEN"]}'

您也可以传递自定义键值对,并在onMessageReceived方法中获取它们。

注意:在这种方法中,您必须创建将在系统托盘中可见的通知。示例代码看起来像:

// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);

你可以在官方文档中找到更多关于这方面的信息

希望这能有所帮助

相关内容

  • 没有找到相关文章

最新更新