Notification not showing but code working fine












0














there's no error in my code yet the notification won't appear in my Android phone (I had try Android 8.0 &9.0). I want the notification to be displayed once it detects the value from firebase is below 50. I do even try the most basic code by clicking the button to call notification yet still no working..



Please help me out,Thanks alot



Here's my code



public class IService extends Service {
private final String CHANNEL_ID="personal_notifications";
private final int NOTIFICATION_ID=001;

public IService() {
}

NotificationManagerCompat notificationManager;
DatabaseReference database=FirebaseDatabase.getInstance().getReference();

public void onCreate(){
super.onCreate();
notificationManager=NotificationManagerCompat.from(this);

database.child("Moisture").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot noteDataSnapshot: dataSnapshot.getChildren()){
Integer value=noteDataSnapshot.getValue(Integer.class);
if(value<50){
displayNotification();
}

}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

public void displayNotification(){
createNotificationChannel();
Intent landingIntent= new Intent(this,MainGarden.class);
landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent landingPendingIntent= PendingIntent.getActivity(this,0,landingIntent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
builder.setSmallIcon(R.drawable.alarm);
builder.setContentTitle("Alert! ");
builder.setContentText(" There is a strong vibration detected on the Front Door");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setContentIntent(landingPendingIntent);
builder.setAutoCancel(true);

NotificationManagerCompat notificationManagerCompat= NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());

}
private void createNotificationChannel()
{
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
CharSequence name= "Personal Notifications";
String description = "Include all the personal notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

notificationChannel.setDescription(description);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}


@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}









share|improve this question





























    0














    there's no error in my code yet the notification won't appear in my Android phone (I had try Android 8.0 &9.0). I want the notification to be displayed once it detects the value from firebase is below 50. I do even try the most basic code by clicking the button to call notification yet still no working..



    Please help me out,Thanks alot



    Here's my code



    public class IService extends Service {
    private final String CHANNEL_ID="personal_notifications";
    private final int NOTIFICATION_ID=001;

    public IService() {
    }

    NotificationManagerCompat notificationManager;
    DatabaseReference database=FirebaseDatabase.getInstance().getReference();

    public void onCreate(){
    super.onCreate();
    notificationManager=NotificationManagerCompat.from(this);

    database.child("Moisture").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnapshot noteDataSnapshot: dataSnapshot.getChildren()){
    Integer value=noteDataSnapshot.getValue(Integer.class);
    if(value<50){
    displayNotification();
    }

    }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
    });
    }

    public void displayNotification(){
    createNotificationChannel();
    Intent landingIntent= new Intent(this,MainGarden.class);
    landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent landingPendingIntent= PendingIntent.getActivity(this,0,landingIntent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
    builder.setSmallIcon(R.drawable.alarm);
    builder.setContentTitle("Alert! ");
    builder.setContentText(" There is a strong vibration detected on the Front Door");
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setContentIntent(landingPendingIntent);
    builder.setAutoCancel(true);

    NotificationManagerCompat notificationManagerCompat= NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());

    }
    private void createNotificationChannel()
    {
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
    {
    CharSequence name= "Personal Notifications";
    String description = "Include all the personal notifications";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

    notificationChannel.setDescription(description);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(notificationChannel);
    }
    }


    @Override
    public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
    }
    }









    share|improve this question



























      0












      0








      0







      there's no error in my code yet the notification won't appear in my Android phone (I had try Android 8.0 &9.0). I want the notification to be displayed once it detects the value from firebase is below 50. I do even try the most basic code by clicking the button to call notification yet still no working..



      Please help me out,Thanks alot



      Here's my code



      public class IService extends Service {
      private final String CHANNEL_ID="personal_notifications";
      private final int NOTIFICATION_ID=001;

      public IService() {
      }

      NotificationManagerCompat notificationManager;
      DatabaseReference database=FirebaseDatabase.getInstance().getReference();

      public void onCreate(){
      super.onCreate();
      notificationManager=NotificationManagerCompat.from(this);

      database.child("Moisture").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      for(DataSnapshot noteDataSnapshot: dataSnapshot.getChildren()){
      Integer value=noteDataSnapshot.getValue(Integer.class);
      if(value<50){
      displayNotification();
      }

      }
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
      });
      }

      public void displayNotification(){
      createNotificationChannel();
      Intent landingIntent= new Intent(this,MainGarden.class);
      landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
      PendingIntent landingPendingIntent= PendingIntent.getActivity(this,0,landingIntent,PendingIntent.FLAG_ONE_SHOT);
      NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
      builder.setSmallIcon(R.drawable.alarm);
      builder.setContentTitle("Alert! ");
      builder.setContentText(" There is a strong vibration detected on the Front Door");
      builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
      builder.setContentIntent(landingPendingIntent);
      builder.setAutoCancel(true);

      NotificationManagerCompat notificationManagerCompat= NotificationManagerCompat.from(this);
      notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());

      }
      private void createNotificationChannel()
      {
      if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
      {
      CharSequence name= "Personal Notifications";
      String description = "Include all the personal notifications";
      int importance = NotificationManager.IMPORTANCE_DEFAULT;

      NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

      notificationChannel.setDescription(description);

      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationManager.createNotificationChannel(notificationChannel);
      }
      }


      @Override
      public IBinder onBind(Intent intent) {
      // TODO: Return the communication channel to the service.
      throw new UnsupportedOperationException("Not yet implemented");
      }
      }









      share|improve this question















      there's no error in my code yet the notification won't appear in my Android phone (I had try Android 8.0 &9.0). I want the notification to be displayed once it detects the value from firebase is below 50. I do even try the most basic code by clicking the button to call notification yet still no working..



      Please help me out,Thanks alot



      Here's my code



      public class IService extends Service {
      private final String CHANNEL_ID="personal_notifications";
      private final int NOTIFICATION_ID=001;

      public IService() {
      }

      NotificationManagerCompat notificationManager;
      DatabaseReference database=FirebaseDatabase.getInstance().getReference();

      public void onCreate(){
      super.onCreate();
      notificationManager=NotificationManagerCompat.from(this);

      database.child("Moisture").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      for(DataSnapshot noteDataSnapshot: dataSnapshot.getChildren()){
      Integer value=noteDataSnapshot.getValue(Integer.class);
      if(value<50){
      displayNotification();
      }

      }
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
      });
      }

      public void displayNotification(){
      createNotificationChannel();
      Intent landingIntent= new Intent(this,MainGarden.class);
      landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
      PendingIntent landingPendingIntent= PendingIntent.getActivity(this,0,landingIntent,PendingIntent.FLAG_ONE_SHOT);
      NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
      builder.setSmallIcon(R.drawable.alarm);
      builder.setContentTitle("Alert! ");
      builder.setContentText(" There is a strong vibration detected on the Front Door");
      builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
      builder.setContentIntent(landingPendingIntent);
      builder.setAutoCancel(true);

      NotificationManagerCompat notificationManagerCompat= NotificationManagerCompat.from(this);
      notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());

      }
      private void createNotificationChannel()
      {
      if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
      {
      CharSequence name= "Personal Notifications";
      String description = "Include all the personal notifications";
      int importance = NotificationManager.IMPORTANCE_DEFAULT;

      NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

      notificationChannel.setDescription(description);

      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationManager.createNotificationChannel(notificationChannel);
      }
      }


      @Override
      public IBinder onBind(Intent intent) {
      // TODO: Return the communication channel to the service.
      throw new UnsupportedOperationException("Not yet implemented");
      }
      }






      android firebase notification-channel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 11:30









      Aniruddh Parihar

      2,15911027




      2,15911027










      asked Nov 20 at 6:01









      Jesscy Tey

      34




      34
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Follow this link for the correct answer. Did you add google.json file created in Firebase console. If not, then first create and add json to your project.



          In Android for Push notification, you can use Firebase notification. It's easy way to add notification functionality in Android app.



          https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/



          Open this link and follow these steps to implement push notification.






          share|improve this answer























          • Yeap, the connection works correctly ady. Juz the notification doesnt work
            – Jesscy Tey
            Nov 20 at 6:14











          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%2f53387099%2fnotification-not-showing-but-code-working-fine%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Follow this link for the correct answer. Did you add google.json file created in Firebase console. If not, then first create and add json to your project.



          In Android for Push notification, you can use Firebase notification. It's easy way to add notification functionality in Android app.



          https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/



          Open this link and follow these steps to implement push notification.






          share|improve this answer























          • Yeap, the connection works correctly ady. Juz the notification doesnt work
            – Jesscy Tey
            Nov 20 at 6:14
















          0














          Follow this link for the correct answer. Did you add google.json file created in Firebase console. If not, then first create and add json to your project.



          In Android for Push notification, you can use Firebase notification. It's easy way to add notification functionality in Android app.



          https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/



          Open this link and follow these steps to implement push notification.






          share|improve this answer























          • Yeap, the connection works correctly ady. Juz the notification doesnt work
            – Jesscy Tey
            Nov 20 at 6:14














          0












          0








          0






          Follow this link for the correct answer. Did you add google.json file created in Firebase console. If not, then first create and add json to your project.



          In Android for Push notification, you can use Firebase notification. It's easy way to add notification functionality in Android app.



          https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/



          Open this link and follow these steps to implement push notification.






          share|improve this answer














          Follow this link for the correct answer. Did you add google.json file created in Firebase console. If not, then first create and add json to your project.



          In Android for Push notification, you can use Firebase notification. It's easy way to add notification functionality in Android app.



          https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/



          Open this link and follow these steps to implement push notification.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 6:11









          Pang

          6,8601563101




          6,8601563101










          answered Nov 20 at 6:11









          S.soni

          563




          563












          • Yeap, the connection works correctly ady. Juz the notification doesnt work
            – Jesscy Tey
            Nov 20 at 6:14


















          • Yeap, the connection works correctly ady. Juz the notification doesnt work
            – Jesscy Tey
            Nov 20 at 6:14
















          Yeap, the connection works correctly ady. Juz the notification doesnt work
          – Jesscy Tey
          Nov 20 at 6:14




          Yeap, the connection works correctly ady. Juz the notification doesnt work
          – Jesscy Tey
          Nov 20 at 6:14


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53387099%2fnotification-not-showing-but-code-working-fine%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







          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out