Search..

Tuesday, June 28, 2022

How to Create Notifiaction in Android another way it means using Application class


private void createNotication(String channe_id, String discription) {

NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
channe_id);
builder.setPriority(NotificationManager.IMPORTANCE_HIGH);
builder.setContentTitle("Location running");
builder.setContentText(discription);
builder.setSmallIcon(R.mipmap.ic_launcher_round);
builder.setAutoCancel(true);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(9, builder.build());

} 

Application calss


public class App extends Application {
public static final String channe_id="Location";
public static final String channel_id2="Backgroundlocation";

@Override
public void onCreate() {
super.onCreate();
SetChannelId(channe_id,"Location");
SetChannelId(channel_id2,"Background Location");

}

private void SetChannelId(String channleId,String channelName)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new
NotificationChannel(channleId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setLockscreenVisibility(1);
channel.setLightColor(getColor(R.color.purple_700));

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
}
}
}

No comments:

Post a Comment