Firebase Push Notifications with images


#1

I am trying to do a push notification with images but not sure what i am doing wrong below is what i did …

require("./bundle-config");
var app = “tns-core-modules/application”;
var platform = “tns-core-modules/platform”;
var ImageModule = (“tns-core-modules/ui/image”);
const application = require(“application”);
const imageSource = require(“image-source”);
var dialogs = require(“ui/dialogs”);
var firebase = require(“nativescript-plugin-firebase”);
var utils = require(“utils/utils”);

var context = utils.ad.getApplicationContext(); // get a reference to the application context in Android

function callback(message) {

var title = message.title;
var message = message.body;
var img_url = message.img;
var soundUri = android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_NOTIFICATION);
var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);

mainIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);

var pendingIntent = android.app.PendingIntent.getActivity(context, 0, mainIntent, android.app.PendingIntent.FLAG_ONE_SHOT);

var builder = new android.app.Notification.Builder(context);
builder.setContentTitle(title)
    .setAutoCancel(true)
    .setContentText(message)
    .setContentIntent(pendingIntent)
    .setSound(soundUri)
    //.setSmallIcon(android.R.drawable.logo);

imageSource.fromUrl(img_url)
    .then(function (res) {
      //console.log("Image successfully loaded");

        builder.setStyle(new android.app.Notification.BigPictureStyle().bigPicture(res));

        var manager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());

    }, function (error) {
        //console.log("Error loading image: " + error);
    });

}

var initObject = {
onMessageReceivedCallback: callback
};

firebase.init(initObject)
.then(function (instance) {
console.log(“firebase.init done”);
}, function (error) {
console.log("firebase.init error: " + error);
});

application.start({ moduleName: “login-page/login” });


How to send Image through firebase
Settings of push notifications via Firebase Cloud Messaging (No sound, led light, badge, vibration)
#2

@shady I just had a deep look in the docs, what you are doing should work if you have declared the custom messaging service in your manifest.


#3

You’ll have to send a notification which only contains the data.
(else it will get displayed automatically)

Then you could use something like local-notifications plugin to create your notification with images.

It is easier to send notifications with images in one signal notification plugin.

But i would recommend the firebase way.