【问题标题】:Display notification when app is closed in android在android中关闭应用程序时显示通知
【发布时间】:2020-11-05 18:53:52
【问题描述】:

即使应用程序关闭/在后台/前台,我也需要在特定日期显示本地通知。

当应用程序处于前台或后台时,我成功显示通知。但如果应用程序关闭,则什么都不会发生。

在此示例中,当我单击按钮时,我会在 2020 年 11 月 8 日 15 点显示警报。 这是我的代码:

  1. 带广播接收器的类接收器
class Receiver: BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent?) {
        val reqCode = 0
        val intent =  Intent(context, MainActivity::class.java)
        showNotification(context, "Title", "This is the message to display", intent, reqCode)
    }


    fun showNotification(context: Context, title: String?, message: String?, intent: Intent?, reqCode: Int) {

        val pendingIntent = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT)
        val CHANNEL_ID = "channel_name" // The id of the channel.

        val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_delete)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name: CharSequence = "Channel Name" // The user-visible name of the channel.
            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            notificationManager.createNotificationChannel(mChannel)
        }
        notificationManager.notify(reqCode, notificationBuilder.build()) // 0 is the request code, it should be unique id
        Log.d("showNotification", "showNotification: $reqCode")
    }
}
  1. 带有警报管理器的 MainActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {

            val alarms = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager

            val receiver = Receiver()
            val filter = IntentFilter("ALARM_ACTION")
            registerReceiver(receiver, filter)

            val intent = Intent("ALARM_ACTION")
            intent.putExtra("param", "My scheduled action")
            val operation = PendingIntent.getBroadcast(this, 0, intent, 0)
            val calendar = Calendar.getInstance()
            calendar.set(2020, 10, 8, 15, 0) // 8 november 2020 
                                                // 15h(3pm)
            
            alarms.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, 
           operation)
                                                                        
        }

    }
}
  1. 清单
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Receiver"/>
    </application>

我提醒这个例子即使应用程序关闭也需要工作。 您可以用 java 或 kotlin(首选 Kotlin)回答。 如果可以的话,我会喜欢完整的例子。

提前致谢。

【问题讨论】:

    标签: java android kotlin android-notifications alarmmanager


    【解决方案1】:

    您的清单似乎没有用于设置警报的uses-permission

    基本上将此行添加到您的清单中:

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    

    我认为它是在 xml 的 &lt;application&gt; 标记上方添加的

    Reference

    编辑 This stackoverflow answer 似乎有如何正确添加到清单

    【讨论】:

    • 感谢您的回答。我添加了这个,我尝试了 stackoverflow 的答案。不要为我工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多