Notification not showing but code working fine

106 Views Asked by At

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");
    }
}
1

There are 1 best solutions below

1
On

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.