我正在使用两个按钮实现自定义通知布局,但为什么它的显示像Image
以及如何按下通知按钮点击事件
我做错了什么
我想像这样显示通知
通知类
public class NotificationService {
public static final String NOTIFICATION_CHANNEL_ID = "1";
public static final String NOTIFICATION_CHANNEL_NAME = "watistant_notification";
Context context;
public NotificationService(Context context) {
this.context = context;
}
public void callNotification() {
Intent intent = new Intent(context, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews notification_layout_small = new RemoteViews(context.getPackageName(),R.layout.notification_layout_small);
RemoteViews notification_layout = new RemoteViews(context.getPackageName(),R.layout.notification_layout);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo_circle)
.setContentTitle("Reminder")
.setContentText("You are dehydrating..........")
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notification_layout_small)
.setCustomBigContentView(notification_layout)
.setContentIntent(pendingIntent)
.setSound(Settings.System.DEFAULT_ALARM_ALERT_URI);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
builder.setContentIntent(pendingIntent);
NotificationManagerCompat.from(context).notify((int) System.currentTimeMillis(), builder.build());
}
}
检查以下代码行,这将有助于您触发按钮单击通知单击
在callNotification()方法中添加以下代码
Notification notification = null;
Intent skip = new Intent(mContext, NotificationActionService.class);
skip.setAction(101);//replace with your custom value
skip.putExtra("test", "test");
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(mContext, 12345, skip, PendingIntent.FLAG_UPDATE_CURRENT);
Intent drink = new Intent(mContext, NotificationActionService.class);//attach all keys starting with wzrk_ to your notification extras
drink.setAction(102);//replace with your custom value
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(mContext, 123456, drink, PendingIntent.FLAG_UPDATE_CURRENT);
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(soundUri)
.setChannelId(channelId)
.addAction("your icon", "skip", pendingIntentYes) //Action Button 1, update the ic_launcher with a image present in your
.addAction("your icon", "dring",pendingIntentNo) // Action Button 2, maximum 3 allowed
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
public class NotificationActionService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle answerBundle = intent.getExtras();
switch (action){
case 101:
Intent intent1 = new Intent(context,MainActivity.class);
intent1.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
break;
case 102:
break;
}
NotificationUtils.clearNotifications(context);
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
}
}
注意:别忘了在清单中声明
<receiver android:name=".NotificationActionService" />