我需要Android通知的代码以及收到通知的时间。
我正在使用以下代码在我的应用中测试通知
public void onPageFinished(WebView view, String url) {
Log.d("App Loaded ==", "Do");
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("testing").setContentText("This is testing").
setContentTitle("time notification").setSmallIcon(R.drawable.ic_notification).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
请提出为什么这对页面完成事件不起作用。没有通知被触发。
答案 0 :(得分:0)
您必须从测试应用程序的位置检查 android版本。
如果其 Oreo + ,则必须添加通知频道,否则将不会收到通知。
,然后将Notification类替换为NotificationCompat。
尝试使用此代码,您一定会收到通知。
/usr/bin
如果要在通知到来时记录时间,则可以在Onreceived内添加1行代码:NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(artist)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCustomContentView(contentView)
.setColor(ctx.getResources().getColor(R.color.colorPrimary))
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);
if (notificationManager != null) {
notification =builder.build();
notificationManager.notify(1, builder.build());
}