Send push notification with ktor

Viewed 633

I made a backend with Ktor framework for a android app and my question is:

How to send push notification from server to phone? What tools could I use for this?

I would like to send notifications that everyone will receive and that just given users will receive.

I am not familiar with Ktor, but my goal with this project is to learn.

2 Answers

Steps:

  • Setup your Android app on Firebase console.
  • Get your Server key from Project Settings -> Cloud Messaging -> Server Key

  • Get the FCM token from the mobile app and store on server database.

  • Trigger the notification on mobile app using the firebase API: https://fcm.googleapis.com/fcm/send

  • Sample for Notification Request: You can use data,notification or both.

    { "registration_ids": ["dfYSqZkGTPevi6gHY65F8J:APA91bF9e6vs2BOjgm8yVj3gdS1R_WHQI50I29uEXw01HDbVA1S67i78cBOXy4FubcfvvGerq36WPOuBggdxBZsF_EE0XHa3YSC6MbEDSvOIlQRypLSDhaOjvIiPawOEt7jGF98DjOik"], "data": { "title": "Hello", "body": "User " }, "notificatoin": { "title": "Hello", "body": "User " } }

use pusher notification like :

 import com.pusher.pushnotifications.PushNotifications;

Then register the Device Token at the appropriate point in your application lifecycle and subscribe your device to an interest hello:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PushNotifications.start(getApplicationContext(), "YOUR_INSTANCE_ID");
    PushNotifications.addDeviceInterest("hello");
}

it's very convenient if you don't have that much experience

Related