Android-从通知启动活动

时间:2020-04-17 11:05:59

标签: android kotlin android-notifications

我正在编写一个时间跟踪器应用程序,该应用程序将启动未绑定的前台服务,以使用户了解所经过的时间。 服务运行顺畅,所有功能都极具魅力……除了一件事! 当用户单击通知时,应开始应用程序的主要活动。 根据Android的文档(https://developer.android.com/training/notify-user/navigation),此代码应该可以运行,但是它只是启动了该应用的Android设置活动。

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Log.d(TAG, "Started")
    isRunning = true
    val channelID = createNotificationChannel()
    val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent ->
        PendingIntent.getActivity(this, 0, notificationIntent, FLAG_UPDATE_CURRENT)
    }

    val notification: Notification = NotificationCompat.Builder(this, channelID)
        .setContentTitle(CHANNEL_NAME)
        .setContentText("My wonderful Text")
        .setPriority(PRIORITY_LOW)
        .setContentIntent(pendingIntent)
        .build()
    startForeground(FOREGROUND_ID, notification)
    timer.scheduleAtFixedRate(TimedTask(), 0, 1000)
    return super.onStartCommand(intent, flags, startId)
}

private fun createNotificationChannel(): String{
    val chan = NotificationChannel(CHANNEL_ID,
        CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE)
    chan.lightColor = Color.BLUE
    chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    service.createNotificationChannel(chan)
    return CHANNEL_ID
}

manifest.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.maybe.tima">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".TimerService"
            android:label="@string/app_name"
            />
    </application>
</manifest>

为什么会发生这种情况?非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

为了使您的代码正常工作,我所做的唯一更改-是在您的notificationBulider中添加方法“ setSmallIcon”(但我在官方文档中找不到关于这种方法的这种影响):

val notification: Notification = NotificationCompat.Builder(this, channelID)
        .setContentTitle(CHANNEL_NAME)
        .setContentText("My wonderful Text")
        .setPriority(PRIORITY_LOW)

        .setSmallIcon(R.drawable.ic_launcher_background) // line added

        .setContentIntent(pendingIntent)
        .build()
相关问题