Launch Android notification at startup

This question is based on my previous question: Android - set a notification so that you never leave

Now I understand that I need to start something at startup in order to trigger a notification.

From my previous question, it seems I can take two routes. Either start the service, or use BroadcastReceiver. I think that starting the service is a bit too much (but what I know). I am stuck walking the BroadcastReceiver route. It works in general, but when I take my code for notification, I get a bunch of errors.

This is my code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("My notification") .setContentText("Hello World!"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, ResultActivity.class); //Intent.putExtra("My Notification"); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(i, mBuilder.build()); 

My mistakes: enter image description here

Any ideas on this?

Summary: Is a service or broadcast receiver better (in this situation)? How can I solve these errors in my code (when you hover over that they are undefined)?

0
source share
1 answer

Replace all instances of this with error strings with context .

BroadcastReciever does not implement a context other than Activity and Service. All methods in which you have an error require a context instance.

+3
source

Source: https://habr.com/ru/post/1384310/


All Articles