FCM Messaging using Nativescript + Angular

android

#1

I want to create an IntentService using NativeScript + Angular app such that the service will generate FCM Notifications while app is not running. Here is some code which i have in Java for the same application which pushes notification using GCM.

public class MyBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setClass(context, MyService.class)));
    setResultCode(Activity.RESULT_OK);
}}

Here is how my IntentService looks like

public class MyIntentService extends IntentService {

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM";

public MyIntentService() {
    super("MyIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that GCM will be
         * extended in the future with new message types, just ignore any message types you're
         * not interested in, or that you don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification("Deleted messages on server: " + extras.toString());
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // Post notification of received message.
            String message = intent.getStringExtra("Sample");
            sendNotification(message);
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    MyBroadcastReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Log.i(TAG, "Inside SendNotification");
    try {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(Constants.MESSAGE, msg);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        String[] lines = msg.split(System.getProperty("line.separator"));
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this)
                .setSmallIcon(R.drawable.store_icon)
                .setContentTitle("Sample Notification")
                .setAutoCancel(true)
                .setContentText("Text");


        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);

        mBuilder.setVibrate(null);
        mBuilder.setContentIntent(contentIntent);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    } catch (Exception e) {
        Log.i(TAG, e.getMessage());
    }

}
}

Can similar code be implemented using NativeScript? Any samples available?


#2

You can implement almost anything, access any API with NativeScript.

To learn more refer this doc.


#3

Sure, please take a look at this plugin: https://github.com/NativeScript/push-plugin
The NativeScript push plugin is good if you only need notifications. If you need more from the Firebase app, then definitely use https://github.com/EddyVerbruggen/nativescript-plugin-firebase which is great and has a lot more features from Firebase.


#4

I am already using nativescript-plugin-firebase. However reason i want to create an IntentService is to get notifications even when my application is not running. Either in foreground and background. Hence there has to be a service running to capture the notification message.