Implementing Android Notifications Programmatically in Firebase

3.9k views Asked by At

Sending notifications in android without a backend, we have to write these lines of code in android studio:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Intent intentViewPost = new Intent(PostsActivity.this,BlogPostsView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(PostsActivity.this,(int) System.currentTimeMillis(), intentViewPost,0);

Notification notification = new Notification.Builder(getBaseContext())
        .setSmallIcon(R.mipmap.ic_app_logo)
        .setContentTitle(getResources().getString(R.string.app_name))
        .setContentText(postTitle)
        .setContentIntent(pendingIntent)
        .build();

manager.notify(111,notification);

How can one do the same thing in Firebase without using the console.

1

There are 1 answers

1
ziLk On

Send messages to specific devices

To send messages to specific devices, set the to the registration token for the specific app instance

curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>"  -X POST -d '{ "data": { "score": "5x1","time": "15:10"},"to" : "<registration token>"}' https://fcm.googleapis.com/fcm/send

Send messages to topics

here the topic is : /topics/foo-bar

curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>"  -X POST -d '{ "to": "/topics/foo-bar","data": { "message": "This is a Firebase Cloud Messaging Topic Message!"}}' https://fcm.googleapis.com/fcm/send

Send messages to device groups

Sending messages to a device group is very similar to sending messages to an individual device. Set the to parameter to the unique notification key for the device group

curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>"  -X POST -d '{"to": "<aUniqueKey>","data": {"hello": "This is a Firebase Cloud Messaging Device Group Message!"}}' https://fcm.googleapis.com/fcm/send

For more information, you check on these links.