亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

應用程序在通知點擊后從后臺打開后運行功能

應用程序在通知點擊后從后臺打開后運行功能

慕標琳琳 2022-12-21 12:41:42
我對 Android 開發還很陌生。當應用程序處于后臺時,我已經能夠收到彈出通知。當我點擊它時,它成功加載了應用程序備份。但是,我想從頁面加載警報,但僅當它從通知點擊打開時才加載。下面是生成通知的代碼。任何幫助,將不勝感激。private void getNotificationForPasswordChange() {    NotificationManager mNotificationManager =            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {        CharSequence name = "Hello";// The user-visible name of the channel.        int importance = NotificationManager.IMPORTANCE_HIGH;        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);        if (mNotificationManager != null)            mNotificationManager.createNotificationChannel(mChannel);    }    Bitmap icon = BitmapFactory.decodeResource(getResources(),            R.mipmap.ic_launcher);    Intent i=new Intent(this, MainActivity.class);    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);    PendingIntent mainIntent = PendingIntent.getActivity(this, 0,            i, PendingIntent.FLAG_UPDATE_CURRENT);    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)            .setContentTitle("Pronto Tracker")            .setTicker("Pronto Tracker")            .setContentText("Cannot connect to server. Location is not being updated.")            .setSmallIcon(R.mipmap.ic_pronto_logo)            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))            .setOngoing(true).setContentIntent(mainIntent).                    build();    mNotificationManager.notify(Constants.PASSWORD_CHANGE_NOTIFICATION_ID, notification);}
查看完整描述

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

      }

    }


查看完整回答
反對 回復 2022-12-21
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您應該在 MainActivity 上使用 onCreate 函數 添加此代碼來分解您的意圖: Intent receivedIntent = getIntent();



查看完整回答
反對 回復 2022-12-21
  • 2 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號