首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当我的闹钟触发onReceive()时不显示通知

当我的闹钟触发onReceive()时不显示通知
EN

Stack Overflow用户
提问于 2018-09-01 05:41:57
回答 2查看 183关注 0票数 0

当我的警报管理器触发我的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分钟的beign创建,但通知仍然没有显示。

有什么想法吗?

谢谢

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52122889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档