简体   繁体   中英

Turn On Notification Programatically in My Android app

I want to turn On notification Access for my android app programitically.

In some android devices, Notification Access for my app is turned off by default. I want to turn On and turn Off the notification access for my app dynamically in the app itself.

But I don't have any idea on how to enable the service.

Please, provide me your views.

Use that kind of metod on your Activity

 public void sendNotification() {

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity());
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.journaldev.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("Notification title");
    builder.setContentText("Your notification message.");
    builder.setSubText("link for more info.");

    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(1, builder.build());
}

public void cancelNotification() {

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
    nMgr.cancel(1);
}

I don't think that this is possible. There are restrictions for android applications which you can pass only with root.

You can prompt user to set those permissions for proper functioning. Setting those within app sounds malicious. I highly doubt such thing is possible. (Why would google create permission in first place if they can be overridden by app?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM