簡體   English   中英

單擊事件不適用於單挑通知

[英]Click event not working on Heads-up notification

單擊抬頭通知的AcceptReject按鈕時沒有任何反應。

但是當抬頭通知消失並且從通知面板單擊AcceptReject

在 Android 5.1.0 上測試。

Intent acceptIntent = new Intent(this, NotificationReceiver.class);
acceptIntent.setAction("com.android.test.Accept");
PendingIntent acceptPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, acceptIntent, PendingIntent.FLAG_CANCEL_CURRENT);

Intent rejectIntent = new Intent(this, NotificationReceiver.class);
rejectIntent.setAction("com.android.test.Reject");
PendingIntent rejectPendingIntent = PendingIntent.getBroadcast(TestApplication.getAppContext(), 12345, rejectIntent, PendingIntent.FLAG_CANCEL_CURRENT);


NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.fundu);
builder.setContentTitle("Test Notification");
builder.setContentText("Hello");
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setCategory(NotificationCompat.CATEGORY_SERVICE);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.addAction(R.drawable.ic_check_icon, "Accept", acceptPendingIntent);
builder.addAction(R.drawable.ic_action_close, "Reject", rejectPendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

只需設置振動,即可正常工作。

builder.setVibrate(new long[0]);

關鍵是在通知生成器上調用 setContentIntent 並將其傳遞給 PendingIntent。 下面包含帶有解釋每個步驟的注釋的完整代碼。 請參閱名為“這是相關部分”的部分(為了完整起見,包含完整代碼。)

// Use Notification Builder to start things off
// There are other ways of acquiring a Notification Builder; this is just an example
String channelId = "channelId";
String title = "title";
String body = "body";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder
        .setSmallIcon(R.drawable.ic_alarm)
        .setContentTitle(title)
        .setContentText(body);


//--------------- THIS IS THE PERTINENT PART ---------------
// Prepare Intent for creating PendingIntent
Intent intent = new Intent(context, ActivityToStart.class);

// Create Pending Intent
int requestCode= 1234;    // requestCode has to be a unique ID for EACH PendingIntent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
        requestCode,
        PendingIntent.FLAG_UPDATE_CURRENT    // use to prevent re-using current Activity Intent
);
notificationBuilder.setContentIntent(pendingIntent);

// Finally, create the Notification
Notification notification = notificationBuilder.build();

暫無
暫無

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

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