Cannot find symbol NOTIFICATION_SERVICE?
Asked Answered
B

6

13
package com.test.app;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class runOnBoot extends BroadcastReceiver{

   @Override
   public void onReceive(Context context, Intent intent) {

          NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
   }
}

When I try to build the package, it says

compile:
    [javac] Compiling 2 source files to /home/mrburns/Desktop/myapp/bin/classes
    [javac] /home/mrburns/Desktop/myapp/src/com/test/app/runOnBoot.java:14: cannot find symbol
    [javac] symbol  : variable NOTIFICATION_SERVICE
    [javac] location: class runOnBoot
    [javac]           NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    [javac]                                                                                           ^
    [javac] 1 error

BUILD FAILED
Belisle answered 27/7, 2011 at 11:43 Comment(0)
S
30

I found calling this way works:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Supererogatory answered 1/3, 2012 at 9:54 Comment(0)
L
10

This should be Context.NOTIFICATION_SERVICE:

NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Loquat answered 27/7, 2011 at 11:46 Comment(1)
I see that is answer is currently being downvoted while it wasn't before. As it has been written in 2011, I suspect that some API updates make it deprecated though I didn't have time to check.Loquat
V
2

For those using Kotlin, the following line performs the same functionality as the other answers here.

val nm = context?.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
Valerivaleria answered 6/5, 2022 at 16:35 Comment(1)
this solution has helped me. I needed to add the context.Eringo
D
0
NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Deadline answered 20/10, 2016 at 13:43 Comment(1)
In addition to your code please add some text to explain why your answer works or how it is different from previous answers.Lejeune
W
0

This should be

notificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Woolworth answered 20/7, 2023 at 17:4 Comment(0)
C
-2

You should better try this

 NotificationManager nm = (NotificationManager)getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
Cauthen answered 27/7, 2011 at 11:57 Comment(3)
NOTIFICATION_SERVICE is a public static final String. You do not need any instance of Context to use it.Loquat
it can be accessed in both ways :) If you are displaying the notification in Any web Service not in activity then you dont have the Context with you :)Cauthen
You don't need any instance of Context. You statically call it from the class Context.Loquat

© 2022 - 2024 — McMap. All rights reserved.