Sunday, 24 June 2018

Can't create a notification in android studio

I tried to create a notification on android to launch a foreground service. it didn't show the notification after the 'startForground' method, neither on the phone I am using nor on an emulator. so I tried to do it using a NotificationManager, it also didn't work on both of them. this is the code:
Notification.Builder notify = new Notification.Builder(this).setContentText("hello!")
            .setStyle(new Notification.BigTextStyle().bigText("sup!"))
            .setContentTitle("sup").addAction(R.drawable.ic_launcher_foreground,"hello", 
PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
             .setPriority(Notification.PRIORITY_HIGH);

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        notify.setSmallIcon(R.mipmap.ic_launcher);
    }else{
        notify.setSmallIcon(R.drawable.ic_launcher_foreground);
    }

    NotificationManager manager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "132";
        String description = "132";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("132", name, importance);
        channel.setDescription(description);

        manager.createNotificationChannel(channel);
    }

    manager.notify(132, notify.build());

    Toast.makeText(this, "finished", Toast.LENGTH_LONG);
    setLatestAction("finished");

please help.


from Can't create a notification in android studio

No comments:

Post a Comment