0

I get this code from google android developer website, but I'm not sure why I'm getting this error. First I did this in my MainActivity and it works fine, and then I created another at AlarmReceiver.java but error occurred.

error: cannot find symbol method getSystemService(Class)

public class AlarmReceiver extends BroadcastReceiver{ private final String CHANNEL_ID = "Notification";

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

    createNotificationChannel();
    Intent notificationIntent = new Intent(context, MapsActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MapsActivity.class);
    stackBuilder.addNextIntent(notificationIntent);



    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    Notification notification = builder.setContentTitle("Demo App Notification")
            .setContentText("New Notification From Demo App..")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}
private void createNotificationChannel(){
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "NOTIFICATION";
        String description = "DESCRIPTION";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Possible duplicate of [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – Bruno Nov 07 '19 at 16:12
  • @BrunoDM Sorry I'm new to this programming world, may I know what kind of duplicate? Cause I really have no idea regarding this issue. – Wilson LVK Nov 07 '19 at 16:19
  • The duplicated question explains what is a `cannot find symbol` error. Its a compilation error. Your code is trying to call something that doesn't exist! Please, read the duplicated question, and you will undesrtand better =) – Bruno Nov 07 '19 at 16:21

0 Answers0