关闭应用程序后如何接收通知?

时间:2020-04-01 13:09:00

标签: android android-studio notifications

我正在制作一个android应用程序,并且我想在该应用程序关闭时收到通知,该怎么办?我还阅读了其他人在问的帖子,但我不知道如何使它起作用,请您尝试更好地解释一下吗?

我还尝试制作一个countdownTimer,并在计时器到达0时使应用程序执行某些操作,但它不起作用,'因为关闭应用程序时很明显,我也关闭了计时器。

1 个答案:

答案 0 :(得分:0)

使用此代码进行应用通知

NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher) // notification icon
            .setContentTitle("Notification!") // title for notification
            .setContentText("Hello word") // message for notification
            .setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
相关问题