【问题标题】:Notification Timing Issue通知时间问题
【发布时间】:2020-04-02 05:18:02
【问题描述】:

我需要 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 版本上进行测试?从奥利奥开始,您需要为您的Notifications 提供频道。

标签: java android push


【解决方案1】:

您必须从您正在测试应用程序的位置检查 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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多