How to handle with notifications in android?

Multi tool use
Multi tool use












-2















A few weeks ago i started my first Android projekt. Amongst other things this application should send a notification to the user, if gps is activated..



In the emulator it works fine, on personal smartphone i got some misstakes i'll explain at the bottom



    public void notifyTest() {

SharedPreferences preferences = getSharedPreferences("pDashPref", 0);

if(preferences.getBoolean("gpsLed", false)) {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationUtils gpsNotification = new NotificationUtils(this);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
gpsNoteActiv = true;
Notification.Builder nb = gpsNotification.getAndroidChannelNotification("GPS aktiviert", "Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert");
gpsNotification.getManager().notify(101, nb.build());
} else {
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
gpsNoteActiv = false;
gpsNotification.getManager().cancel(101);
}
}
} else {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
gpsNoteActiv = true;
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// if(Benachrichtigung + LED gewählt) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelID)
.setSmallIcon(R.drawable.gpstag)
.setContentTitle("GPS Signal")
.setContentText("Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert")
.setPriority(NotificationCompat.PRIORITY_MAX)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setLights(0xff00ff00, 300, 100)
.setAutoCancel(true);


NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(uniqueID, mBuilder.build());
} else {
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
gpsNoteActiv = false;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(45432);
}
}
}
}
}


With this method i execute the code



 @Override
protected void onStart() {
super.onStart();


final Handler ha = new Handler();
ha.postDelayed(new Runnable() {

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void run() {

System.out.print("Test");
notifyTest();
//countGps();
ha.postDelayed(this, 10000);
}
}, 10000);
}


Maybe not the smartest way but it works. On my personal smartphone i don't get a notification when i activate gps by using Google Maps or something similar. By switching gps on myself everything works fine. By using a second smartphone for testing the application i did not get any notification..










share|improve this question



























    -2















    A few weeks ago i started my first Android projekt. Amongst other things this application should send a notification to the user, if gps is activated..



    In the emulator it works fine, on personal smartphone i got some misstakes i'll explain at the bottom



        public void notifyTest() {

    SharedPreferences preferences = getSharedPreferences("pDashPref", 0);

    if(preferences.getBoolean("gpsLed", false)) {
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationUtils gpsNotification = new NotificationUtils(this);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
    gpsNoteActiv = true;
    Notification.Builder nb = gpsNotification.getAndroidChannelNotification("GPS aktiviert", "Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert");
    gpsNotification.getManager().notify(101, nb.build());
    } else {
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
    gpsNoteActiv = false;
    gpsNotification.getManager().cancel(101);
    }
    }
    } else {
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
    gpsNoteActiv = true;
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    // if(Benachrichtigung + LED gewählt) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelID)
    .setSmallIcon(R.drawable.gpstag)
    .setContentTitle("GPS Signal")
    .setContentText("Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert")
    .setPriority(NotificationCompat.PRIORITY_MAX)
    // Set the intent that will fire when the user taps the notification
    .setContentIntent(pendingIntent)
    .setLights(0xff00ff00, 300, 100)
    .setAutoCancel(true);


    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(uniqueID, mBuilder.build());
    } else {
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
    gpsNoteActiv = false;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(45432);
    }
    }
    }
    }
    }


    With this method i execute the code



     @Override
    protected void onStart() {
    super.onStart();


    final Handler ha = new Handler();
    ha.postDelayed(new Runnable() {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void run() {

    System.out.print("Test");
    notifyTest();
    //countGps();
    ha.postDelayed(this, 10000);
    }
    }, 10000);
    }


    Maybe not the smartest way but it works. On my personal smartphone i don't get a notification when i activate gps by using Google Maps or something similar. By switching gps on myself everything works fine. By using a second smartphone for testing the application i did not get any notification..










    share|improve this question

























      -2












      -2








      -2








      A few weeks ago i started my first Android projekt. Amongst other things this application should send a notification to the user, if gps is activated..



      In the emulator it works fine, on personal smartphone i got some misstakes i'll explain at the bottom



          public void notifyTest() {

      SharedPreferences preferences = getSharedPreferences("pDashPref", 0);

      if(preferences.getBoolean("gpsLed", false)) {
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      NotificationUtils gpsNotification = new NotificationUtils(this);
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
      gpsNoteActiv = true;
      Notification.Builder nb = gpsNotification.getAndroidChannelNotification("GPS aktiviert", "Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert");
      gpsNotification.getManager().notify(101, nb.build());
      } else {
      if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
      gpsNoteActiv = false;
      gpsNotification.getManager().cancel(101);
      }
      }
      } else {
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
      gpsNoteActiv = true;
      Intent intent = new Intent(this, MainActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
      // if(Benachrichtigung + LED gewählt) {
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelID)
      .setSmallIcon(R.drawable.gpstag)
      .setContentTitle("GPS Signal")
      .setContentText("Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert")
      .setPriority(NotificationCompat.PRIORITY_MAX)
      // Set the intent that will fire when the user taps the notification
      .setContentIntent(pendingIntent)
      .setLights(0xff00ff00, 300, 100)
      .setAutoCancel(true);


      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
      notificationManager.notify(uniqueID, mBuilder.build());
      } else {
      if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
      gpsNoteActiv = false;
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.cancel(45432);
      }
      }
      }
      }
      }


      With this method i execute the code



       @Override
      protected void onStart() {
      super.onStart();


      final Handler ha = new Handler();
      ha.postDelayed(new Runnable() {

      @RequiresApi(api = Build.VERSION_CODES.O)
      @Override
      public void run() {

      System.out.print("Test");
      notifyTest();
      //countGps();
      ha.postDelayed(this, 10000);
      }
      }, 10000);
      }


      Maybe not the smartest way but it works. On my personal smartphone i don't get a notification when i activate gps by using Google Maps or something similar. By switching gps on myself everything works fine. By using a second smartphone for testing the application i did not get any notification..










      share|improve this question














      A few weeks ago i started my first Android projekt. Amongst other things this application should send a notification to the user, if gps is activated..



      In the emulator it works fine, on personal smartphone i got some misstakes i'll explain at the bottom



          public void notifyTest() {

      SharedPreferences preferences = getSharedPreferences("pDashPref", 0);

      if(preferences.getBoolean("gpsLed", false)) {
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      NotificationUtils gpsNotification = new NotificationUtils(this);
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
      gpsNoteActiv = true;
      Notification.Builder nb = gpsNotification.getAndroidChannelNotification("GPS aktiviert", "Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert");
      gpsNotification.getManager().notify(101, nb.build());
      } else {
      if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
      gpsNoteActiv = false;
      gpsNotification.getManager().cancel(101);
      }
      }
      } else {
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !gpsNoteActiv) {
      gpsNoteActiv = true;
      Intent intent = new Intent(this, MainActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
      // if(Benachrichtigung + LED gewählt) {
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelID)
      .setSmallIcon(R.drawable.gpstag)
      .setContentTitle("GPS Signal")
      .setContentText("Ihr GPS Signal ist eventuell unbeabsichtigt aktiviert")
      .setPriority(NotificationCompat.PRIORITY_MAX)
      // Set the intent that will fire when the user taps the notification
      .setContentIntent(pendingIntent)
      .setLights(0xff00ff00, 300, 100)
      .setAutoCancel(true);


      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
      notificationManager.notify(uniqueID, mBuilder.build());
      } else {
      if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && gpsNoteActiv) {
      gpsNoteActiv = false;
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.cancel(45432);
      }
      }
      }
      }
      }


      With this method i execute the code



       @Override
      protected void onStart() {
      super.onStart();


      final Handler ha = new Handler();
      ha.postDelayed(new Runnable() {

      @RequiresApi(api = Build.VERSION_CODES.O)
      @Override
      public void run() {

      System.out.print("Test");
      notifyTest();
      //countGps();
      ha.postDelayed(this, 10000);
      }
      }, 10000);
      }


      Maybe not the smartest way but it works. On my personal smartphone i don't get a notification when i activate gps by using Google Maps or something similar. By switching gps on myself everything works fine. By using a second smartphone for testing the application i did not get any notification..







      java android android-intent android-notifications






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 12:10









      Jonas RickeJonas Ricke

      154




      154
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977467%2fhow-to-handle-with-notifications-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977467%2fhow-to-handle-with-notifications-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          p1OLB DljGVl34,itgu6mBlb1O1 v0SnR 9WN9RPclW41 tMo1OqR9F35oA,66LUiDX ifxyubCa04,R1qzXo 0NG YunO
          0KlNyjU 8hiUQJuvKG9H7ZxZ80E

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas