我的警报触发onReceive()时未显示通知

时间:2018-08-31 21:41:57

标签: java android notifications

我正在尝试在警报管理器触发onReceive()方法时弹出通知。这就是我所做的

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
    //Acquire the lock
    wl.acquire(10000);

    startNotification(context);

    wl.release();

}


public void setAlarm(Context context){
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(Activity, "MainActivity.class");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    assert am != null;
    am.set(AlarmManager.RTC_WAKEUP, 60000, pi);
}


private void startNotification(Context context){
    // Sets an ID for the notification
    int mNotificationId = 001;
    NotificationManager notificationManager;
    NotificationCompat.Builder mBuilder;

            // Build Notification , setOngoing keeps the notification always in status bar
    mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("RandomTitle")
                    .setContentText("RandomText")
                    .setOngoing(true);

    // Create pending intent, mention the Activity which needs to be
    //triggered when user clicks on notification(StopScript.class in this case)

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("extra","Extra Notificacion");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);
   // context.startActivity(notificationIntent);

    mBuilder.setContentIntent(contentIntent);


    // Gets an instance of the NotificationManager service
     notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    //Android Oreo
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("notify_001",
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    // Builds the notification and issues it.
    notificationManager.notify(mNotificationId, mBuilder.build());
}

我真的很困惑为什么不显示此通知,我已经测试了警报,并在创建良性警钟1分钟后触发了该警报,但是该通知仍未显示。

有什么想法吗?

谢谢

2 个答案:

答案 0 :(得分:1)

根据Android开发者文档:

  

NotificationCompat.Builder   NotificationCompat.Builder(上下文上下文)   API级别26.1.0中不推荐使用此构造函数。采用   改为使用NotificationCompat.Builder(Context,String)。全部发布   通知必须指定一个NotificationChannel ID。

参考文献here
编辑: 尝试以下操作:

  1. NotificationCompat.Builder保持原样(带有字符串) 代表NotificationChannel)。
  2. 在创建通知通道的地方注释掉if

  3. NotificationManager替换为NotificationManagerCompat如下:

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    
                notificationManager.notify(mNotificationId, mBuilder.build());
    

答案 1 :(得分:1)

来自Android开发者:

  

以Android 8.0(API级别26)为目标时,您必须实现   更多的通知渠道。如果您的targetSdkVersion设置为25或   较低,当您的应用在Android 8.0(API级别26)或更高版本上运行时,   行为与运行Android 7.1(API级别)的设备相同   25)或更低。

因为您的targetSdkVersion是28,所以也必须在channelId构造函数中添加Builder

将代码更改为:

// Build Notification , setOngoing keeps the notification always in status bar
mBuilder = new NotificationCompat.Builder(context, "notify_001") // Add channel ID to the constructor.
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("RandomTitle")
        .setContentText("RandomText")
        .setOngoing(true);