Android 已终止应用程序通知单击“附加方法”无效
我有一个问题Android 已终止应用程序通知单击“附加方法”无效,android,firebase,android-intent,push-notification,Android,Firebase,Android Intent,Push Notification,我有一个问题意图。当应用程序被终止时,putExtra不工作。唤醒和运行模式全部工作。我使用firebase消息服务,getData()方法,挂起意图 private PendingIntent getPendingIntent(int newsID) { Intent intent = new Intent(this, DetailedNewsActivity.class); intent.putExtra("newsID", newsID); TaskStackBui
意图。当应用程序被终止时,putExtra
不工作。唤醒和运行模式全部工作。我使用firebase消息服务,getData()
方法,挂起意图
private PendingIntent getPendingIntent(int newsID) {
Intent intent = new Intent(this, DetailedNewsActivity.class);
intent.putExtra("newsID", newsID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
// All the parents of SecondActivity will be added to task stack.
stackBuilder.addNextIntentWithParentStack(intent);
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
我猜您的onMessageReceived()函数没有被触发。firebase数据消息有两种类型的通知,另一种是通知消息。当前您收到的通知消息不会在终止状态下调用onMessageReceived()。改为使用“数据”消息。这可以由后端开发人员完成。请让他发送数据消息。 仅当应用程序处于前台时,通知消息才会触发onMessageRecieved(),但数据消息会在前台和后台状态下调用onMessageRecieved() 请将有效负载中的“通知”键替换为“数据”键。
有关更多信息,请参阅此链接我想您的onMessageReceived()函数没有被触发。firebase数据消息有两种类型的通知,另一种是通知消息。当前您收到的通知消息不会在终止状态下调用onMessageReceived()。改为使用“数据”消息。这可以由后端开发人员完成。请让他发送数据消息。 仅当应用程序处于前台时,通知消息才会触发onMessageRecieved(),但数据消息会在前台和后台状态下调用onMessageRecieved() 请将有效负载中的“通知”键替换为“数据”键。
有关更多信息,请参阅此链接先检查数据,然后再使用该数据
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
try {
JSONObject json = new JSONObject("{\"data\":"+remoteMessage.getData().toString()+"}");
//decode your json data and use that
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String stingMessage = remoteMessage.getNotification().getBody();
//show the message string as notification or as your want
}
显示通知并添加挂起的意图
Intent myintent = new Intent(this, YourActivityWantToL.class);
myintent.putExtra("message", stingMessage);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(TAG)
.setStyle(new NotificationCompat.BigTextStyle() .bigText(msg))
.setContentText(stingMessage);
mBuilder.setContentIntent(contentIntent);
但如果您的目标是在26或更高版本上运行应用程序,则必须先使用通知频道检查数据,然后再使用该频道
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
try {
JSONObject json = new JSONObject("{\"data\":"+remoteMessage.getData().toString()+"}");
//decode your json data and use that
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
String stingMessage = remoteMessage.getNotification().getBody();
//show the message string as notification or as your want
}
显示通知并添加挂起的意图
Intent myintent = new Intent(this, YourActivityWantToL.class);
myintent.putExtra("message", stingMessage);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(TAG)
.setStyle(new NotificationCompat.BigTextStyle() .bigText(msg))
.setContentText(stingMessage);
mBuilder.setContentIntent(contentIntent);
但如果您的目标是在26或更高版本上运行应用程序,您必须使用通知频道您能详细说明您的问题吗?我使用FirebaseMessagingService、messageEvent.getData()方法发送通知。允许所有通知,请将onclick pending intent设置为启动活动。您的活动是否为启动程序活动,并在挂起的intent上打开?不是子活动您必须将intent传递给splash或启动程序活动,然后您需要导航。因为当应用程序运行时,它工作正常,但在终止应用程序后,挂起的意图将进入启动器活动。请再试一次。您能详细说明您的问题吗?我使用FirebaseMessagingService、messageEvent.getData()方法发送通知。允许所有通知,请将onclick pending intent设置为启动活动。您的活动是否为启动程序活动,并在挂起的intent上打开?不是子活动您必须将intent传递给splash或启动程序活动,然后您需要导航。因为当应用程序运行时,它工作正常,但在终止应用程序后,挂起的意图将进入启动器活动。请再试一次。我使用数据消息我使用数据消息所有通知都允许没有问题此数据方法所有通知都允许没有问题此数据方法