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();
添加回答
舉報