0

i am trying to display a notification that will be triggered on a device reboot using a broadcast receiver and it does not work.. any suggestions where is my code gone wrong?

this is the manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.notificationwork">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:allowBackup="true"
        android:directBootAware="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:targetApi="n">
        <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=".AppReceiver"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

this is my receiver class:

private const val CHANNEL_ID = "1"
private const val NOTIFICATION_ID = 10
private lateinit var manager: NotificationManager

class AppReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Log.d("AppReceiver", intent.action!!)
        manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Notification title from app")
            .setContentText("content text")
            .setAutoCancel(true)
        manager.notify(NOTIFICATION_ID, notification.build())
    }

}

i have created the channel in MainActivity:

private const val CHANNEL_ID = "1"
private lateinit var manager: NotificationManager
class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(
                NotificationChannel(
                    CHANNEL_ID,
                    "appChannel",
                    NotificationManager.IMPORTANCE_HIGH
                )
            )
        }
    }
}

thanks in advance!

Yarin Shitrit
  • 297
  • 4
  • 16
  • Have you tested the `Notification` by itself to make sure that part is right? Did you remember to create the `NotificationChannel` for `CHANNEL_ID` before rebooting; e.g., in your `MainActivity`? – Mike M. May 24 '20 at 20:04
  • Hey, yes i have tested the notification and it does work, i created it in MainActivity, i edited the post and added main activity snippet – Yarin Shitrit May 25 '20 at 05:12
  • And you made sure to launch `MainActivity` at least once after installation, before rebooting? Also, are you testing this on a physical device, or an emulator? If a device, which one, exactly? – Mike M. May 25 '20 at 05:20
  • Yes i launched it atleast once before rebooting, and running on my physical device Xiaomi Note 6 Pro – Yarin Shitrit May 25 '20 at 07:22
  • That manufacturer's devices often have additional restrictions that prevent most third-party apps from doing certain things – e.g., starting at boot, running in the background, etc. – without the user having explicitly allowed it. You'll need to locate the settings for those, and make sure your app is allowed. I've never used such a device, so I can't tell you where to go specifically, but check these posts: https://stackoverflow.com/q/30748107, https://stackoverflow.com/q/37927565, https://stackoverflow.com/a/35770734, https://stackoverflow.com/q/48166206, https://stackoverflow.com/q/55258440 – Mike M. May 25 '20 at 07:29
  • ok so once i turned on Auto-Start option in the app settings it seems to work.. but i mean this is not reliable.. what about other devices?, ive seen in the posts you mentioned people write that there is no way to do it programmatically and only by user interaction but thank you anyway for making me understand this issue – Yarin Shitrit May 25 '20 at 07:48
  • Yep, that's just how it is with that manufacturer. There are several others like that, too. Anyhoo, glad you got it figured out. Cheers! – Mike M. May 25 '20 at 07:55

0 Answers0