Customise push notification

Hi guys do you have an up to date link to this as it does not exist?
(https://backendless.com/documentation/messaging/android/messaging_push_notification_setup_androi.htm)

It is referenced here in this post about how you extend some backendless push services.

If I remember correctly this was aimed for backendless 3, but thinking the same methodology still more or less applies to backendless 5?

Regards

Mike

1 Like

Hi Mike

Take a look a this one: https://backendless.com/docs/android/push_push_notification_setup_androi.html

Regards, Vlad

Hi Vladimir, thanks but this one does not document how you can extend the BackendlessBroadcastReceiver or BackendlessPushService class. I think I need that so that when a user clicks on a push notification I can run some code and push them to a different Android Activity.

I have done this in the past, but wanted a reminder if you have any docs?

Mike

do you use PushNotification Templates?

Here is a doc for handling Buttons Actions https://backendless.com/docs/android/push_android_notification_buttons.html

Yes and no. They are great, but don’t want to be restricted to using just them. And also we don’t normally send a button with the push notification so not sure this solution would work as we cannot rely on buttons being present in the push notification.

Is the extending of the BackendlessPushService not recommended anymore?

I’ve asked our Android devs to take a look at this topic and someone will definitely answer to you as soon as possible.

Regards, Vlad

Thanks Vlad.

Hi Android devs it looks like actually I should extend BackendlessFCMService in my Android project.

However what are the benefits or disadvantages of using that method compared to extending the FirebaseMessagingService class. Confused what is the correct way or will both ways work?

public class MyFirebaseMessagingService FirebaseMessagingService {
}

Mike

If you want to use raw Google Firebase messaging – you should use FirebaseMessagingService and their documentation.

If you prefer all advantages offered by Backendless, you are obligatory in extending the BackendlessFCMService. It doesn’t limit you but helps, taking on it routine actions, and binds with the server-side code, which allow using our messaging templates functionality.

Hi Oleg - well obviously I prefer all the advantages offered by backendless :slight_smile:

If I extend BackendlessFCM service can I still override the onMessageReceived and sendNotification events as this seems to be where you specify which activity controller you can navigate to when you click on a push notification.

Mike

The work with BackendlessFCMService has another workflow.
BackendlessFCMService offers you two methods for overriding.

  /**
   * <p>This method is intended for overriding.
   * <p>The notification payload can be found within intent extras: intent.getStringExtra(PublishOptions.<CONSTANT_VALUE>).
   * @param appContext Application context of current android app.
   * @param msgIntent Contains all notification data.
   * @return Return 'true' if you want backendless library to continue processing, 'false' otherwise.
   */
  public boolean onMessage( Context appContext, Intent msgIntent )
  {
    Log.i( TAG, "Notification has been received by default 'BackendlessFCMService' class. You may override this method in a custom fcm service class which extends from 'com.backendless.push.BackendlessFCMService'. The notification payload can be found within intent extras: msgIntent.getStringExtra(PublishOptions.<CONSTANT_VALUE>)." );
    return true;
  }

  @Override
  public void onDeletedMessages()
  {
    super.onDeletedMessages();
    Log.w( TAG, "there are too many messages (>100) pending for this app or your device hasn't connected to FCM in more than one month." );
  }

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