Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Not getting push notification from firebase using node js

I wanted to get push notification in realtime. I found one node js program. I have created token in the Database. But still I am not able to get any notification in my phone. Please help me with this issue.

Here is the code

public class MyFirebaseMessagingSerivce extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s)
    {
        super.onNewToken(s);
        Log.e("NEW_TOKEN", s);
    }

    public void onMessageReceived(RemoteMessage remoteMessage)
    {
        super.onMessageReceived(remoteMessage);
        Map<String,String> data= remoteMessage.getData();

        String title= data.get("title");
        String body=data.get("body");

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

        String Notification_channel_ID="glucoseapp";

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        {
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel=new NotificationChannel(Notification_channel_ID, "notification",NotificationManager.IMPORTANCE_MAX);

            notificationChannel.setDescription("Testing notification");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationChannel.enableVibration(true);

            notificationManager.createNotificationChannel(notificationChannel);

        }
        NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this,Notification_channel_ID);

        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(android.support.v4.R.drawable.notification_icon_background)
                .setTicker("hello")
                .setContentTitle(title)
                .setContentText(body)
                .setContentInfo("info");

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




}

And here is the node js code.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.helloWorld = functions.database.ref('notification/{id}').onWrite(evt => {
    const payload = {
        notification:{
            title : 'Message from Cloud',
            body : `This is your body`,
            badge : '1',
            sound : 'default'
        }
    };

    return admin.database().ref('FCMtoken').once('value').then(allToken => {
        if(allToken.val()){
            console.log('token available');
            const token = Object.keys(allToken.val());
            return admin.messaging().sendToDevice(token,payload);
        }else{
            console.log('No token available');
        }
    });
})

Comments