Customise push notification

Hi Oleg, OK I got that, but I am missing something in the jigsaw puzzle. I would like to be able to change the destination of the activity in this method. So rather than going to the main activity I would like it to go another activity. Tried below but it still goes to the main activity. Any ideas what am I doing wrong. Understand this might be outside of backendless here, but perhaps a quick glance if I am doing something fundamentally wrong or in the wrong place but would be very helpful.

This is what I have (abbreviated for clarity)

@Override
public boolean onMessage(Context appContext, Intent msgIntent) {

    // confirm yes we get here when message received.

    NotificationManager mNotificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);

    // put the Activity here that you want to open when you click the Notification
    Intent notificationIntent = new Intent(appContext, PushNotification.class);
    PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, notificationIntent, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(appContext,"default");
    notification.setContentTitle(messageTitle).setContentText(message).setSmallIcon(R.drawable.icon);
    notification.setContentIntent(contentIntent);

    mNotificationManager.notify(0, notification.build());

    return true;


}

As i understood, you want to show the message in the notification bar.
Have you tried to do this with the notification template? E.g. you could try to add the button (on the template) and attach your activity to it. Or you want go there at the tap on the notification itself ?
Howbeit, try with the button, it maybe the start point.
ps. at first sight your code is correct, but I didn’t debug it.

Hi Oleg, no I want to go there at the tap of the notification itself.

The problem with the button route on the template is that we don’t use buttons on our push notifications. We just need to be able to click on the notification like normal but go through to a different activity not the main/home activity.

Mike

We’ll discuss the possibility of adding such a feature to our messaging template functionality.
The inner task is [BKNDLSS-20867].

Thanks Oleg.

Also talking about template functionality if you setup a template in the console you can call it very easily with Backendless.Messaging.pushWithTemplate( templateName )

but was wondering if you could pass it optional paramaters on the api call to the template too? That would mean you could set a template for specific customers and then just change parts of the content of the message quite easily. like Backendless.Messageing.pushWithTemplate( templateName, title)

Mike

Please, look through this doc
https://backendless.com/docs/android/push_notification_content.html

I can’t see anything there about passing paramaters to template via api?

Hi Oleg - just posted a related but considered a new topic - can’t get back header values in android

Hi @mike-turner

If you want to redirect the user to another activity, you should create your own service that will extend BackendlessFCMService and manually build the notification. Here is a rough example:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.backendless.Backendless;
import com.backendless.push.BackendlessFCMService;

public class MyFCMService extends BackendlessFCMService {
    @Override
    public boolean onMessage(Context appContext, Intent msgIntent) {
        showNotification();
        return false;
    }

    void showNotification() {
        final NotificationCompat.Builder notificationBuilder;
        Context context = getApplicationContext();
        String channelName = "channelName";
        if (android.os.Build.VERSION.SDK_INT > 25) {
            final String channelId = Backendless.getApplicationId() + ":" + channelName;
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);

            if (notificationChannel == null) {
                notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(notificationChannel);
            }
            notificationBuilder = new NotificationCompat.Builder(context, notificationChannel.getId());
        } else {
            notificationBuilder = new NotificationCompat.Builder(context);
        }

        int appIcon = context.getApplicationInfo().icon;
        if( appIcon == 0 )
            appIcon = android.R.drawable.sym_def_app_icon;

        Intent notificationIntent = new Intent(getApplicationContext(), SecondActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity( context, 0, notificationIntent, 0 );

        notificationBuilder.setContentIntent( contentIntent )
            .setSmallIcon( appIcon )
            .setContentTitle( "contentTitle" )
            .setSubText( "summarySubText" )
            .setContentText( "message" )
            .setWhen( System.currentTimeMillis() )
            .setAutoCancel( true )
            .build();

        final NotificationManagerCompat notificationManager = NotificationManagerCompat.from( context );
        Handler handler = new Handler( Looper.getMainLooper() );
        handler.post(() -> notificationManager.notify( channelName, 0, notificationBuilder.build() ));
    }
}

The SecondActivity is the Activity I want to open when the user click the notification:
Intent notificationIntent = new Intent(getApplicationContext(), SecondActivity.class);

Also don’t forget to define your service in the manifest:

    <service android:name=".MyFCMService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

Best Regards,
Maksym

1 Like

Also please note that you should return false in the onMessage method. Otherwise there will be shown two notifications: yours and from SDK.

I can’t see anything there about passing paramaters to template via api?

You should take a look at Smart Text section:
https://backendless.com/docs/android/push_notification_content.html#smart-text

Also this topic may be helpful for you:

and

Thanks Maksym, going to try that this afternoon. let you know how I get on.

Ah, ok yes, of course, you could use the smart text as like a substitute for passing in a paramater. I think I get that now.

Perfect, that works how I was hoping it would. Many thanks for your code input.

You are welcome, Mike!

Hi Maksym,

Just another quick question on this code you provided…

void showNotification() {
        final NotificationCompat.Builder notificationBuilder;
        Context context = getApplicationContext();
        String channelName = "channelName";

For channelName, can I somehow get this from this in the msgIntent which is passed into the override onMessage. So if a channel is set in the message template I would like to pick it up here and I can’t find a way of doing that?

Hi @mike-turner

The channel name is the same as template name.

To get the template name use the following code:

final String templateName = intent.getStringExtra( PublishOptions.TEMPLATE_NAME );

And to get the template object with all the information about push notification, use:

AndroidPushTemplate template = PushTemplateHelper.getPushNotificationTemplates().get( templateName );

Best Regards,
Maksym

Hi thanks for that, it makes sense.

Strange thing is template name is returning as ‘ImmediateMessage’

In the backendless console I have setup a template called ‘android_developer_phone_test’ , created and added a channel called testChannel to this template.

So you would expect it to return ‘android_developer_phone_test’ correct ? I don’t have any channels or templates called ‘ImmediatMessage’ so confused as where that is coming from.

Mike

You receive the “ImmediateMessage” name if you send the notification from Console.
When you send the notification using API
Backendless.Messaging.sendWithTemplate() you will receive the real template name.

Best Regards

Ah ok, no probs, I can just send it through the header in the console as a work around.