簡體   English   中英

帶有待定 URL-Intent 的 Android 通知 - 僅通過單擊通知

[英]Android Notification with pending URL-Intent - only by clicking the notification

我正在嘗試為通知創建一個掛起的 URL-Intent,如果用戶單擊通知,則會調用該通知。

問題是 Intent 是通過創建通知來調用的,因此 url 會立即在 Web 瀏覽器中被調用。 我想防止這種情況,但不知道如何?

這是我的代碼

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.com"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(MyApp.getContext(), 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApp.getContext(), "MyApp")
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("my App")
                    .setContentText("Click here to call the URL")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    ;

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MyApp.getContext());

notificationManager.notify(getNotificationId(), builder.build());

如何防止在“通知”調用后執行意圖?

只有當用戶單擊通知時才應執行意圖。

try this:
public void notification()
{
 Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
 notificationIntent.setData(Uri.parse("https://www.google.com/"));

@SuppressLint("WrongConstant") PendingIntent pending = 
PendingIntent.getActivity(this, 0, notificationIntent, 
Intent.FLAG_ACTIVITY_NEW_TASK);
String channelId = getString(R.string.app_name);
NotificationChannel notificationChannel = null;
NotificationManagerCompat notificationManager = 
 NotificationManagerCompat.from(getApplicationContext());

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    notificationChannel = new NotificationChannel(channelId, channelId,  
NotificationManager.IMPORTANCE_DEFAULT);
    notificationChannel.setDescription(channelId);
    notificationChannel.setSound(null, null);
    notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new 
NotificationCompat.Builder(getApplicationContext(), channelId)
        .setSmallIcon(R.drawable.ic_launcher_background)
        .setContentTitle("my App")
        .setContentText("Click here to call the URL")
        .setStyle(new NotificationCompat.BigTextStyle().bigText("...."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pending)
        .setAutoCancel(true)
        ;


  notificationManager.notify(0, builder.build());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM