我需要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);
}
请提示为什么这对页面完成事件不起作用。没有触发通知。
您必须在测试应用程序的位置检查android版本。>>
如果它的Oreo +
,那么您必须添加通知频道,否则您将不会收到通知。并且请使用NotificationCompat取代Notification类,而不再使用。
尝试使用此代码,您一定会得到通知。
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()); }
并且,如果您想在通知到来时记录时间,则可以在Onreceived内添加1行代码:
Log.d(TAG, "Time: "+System.currentTimeMillis());