2 回答
TA贡献1844条经验 获得超8个赞
您可以传递带有通知 PendingIntent 的警报消息。在 PendingIntent .putExtra() 中添加要显示为警报的消息或值,并在 PendingIntent 中指定要以对话框或任何形式显示警报的活动。
Intent intent = new Intent(Application.getAppContext(), MainActivity.class);
intent.putExtra("is_notification", true);
intent.putExtra("alert_message", "Hello World!");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
之后将 PendingIntent 添加到您的通知中。您需要做的第二件事是在用户点击通知时从 Intent 获取数据。在您的 MainActivity 中添加以下代码以从 Intent 获取数据:-
if (getIntent() != null) {
String message = getIntent().getStringExtra("alert_message");
boolean isNotification = getIntent().getBooleanExtra("is_notification", false);
if(is_notification){
// show alert
}
}
TA贡献1890条经验 获得超9个赞
您应该在 MainActivity 上使用 onCreate 函数 添加此代码来分解您的意图: Intent receivedIntent = getIntent();
添加回答
举报