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?
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?
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 {
}
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
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.
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.
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)
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:
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: