Custom Push Notification and handle onClick

Hi!
After I have achieved to get the notifications working, I wanted to know if there is any posibility of change notification layout, icon etc.
And how to handle the click on the notification, all the docs I’m reading talk about handle this things in the’ onReceive 'method, but BackendlessBroadcastReceiver does not have this method (it can’t be overriden).

Hi Jacob,

The onReceive method is there (but you’re right, it cannot be overridden):

You can make a copy of BackendlessBroadcastReceiver and modify it as you wish. Then you’d register your own class in the Android manifest.

Regards,
Mark

Thanks, I will try and comment the results!

Hi, Jacob!

Another way is to override onMessage() method. Here is an example:

public class PushReceiver extends BackendlessBroadcastReceiver
{
  @Override
  public boolean onMessage( Context context, Intent intent )
  {
    String message = intent.getStringExtra( "message" );
    Log.i( "PUSH_NOTIFICATION", "Push message received. Message: " + message );
    Toast.makeText( context, "Push message received. Message: " + message, Toast.LENGTH_LONG ).show();
    return false;
  }
}

Then you should declare PushReceiver in manifest.
Hope it helps.
best regards,
Alex

Hello!
yes, thanks for the advice, but I’m still trying to figure how to handle the notification click (not when the notification is received, but when you click it, and then do some action)

Could you please post your solution here when you find one?

Yes!
Finally I found the Alexandr’s answer the way to proceed.
I can Override onMessage() and do there what I want:

public class PushNotificationReceiver extends BackendlessBroadcastReceiver { 
 private static final int NOTIFICATION_ID = 1; 
 
 @Override 
 public boolean onMessage(Context context, Intent intent) 
 { 
 String message = intent.getStringExtra("message"); 
 
 NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
 
 //Here you put the Activity that you want to open when you click the Notification 
 //(and you can pass some Bundle/Extra if you want to add information about the Notification) 
 Intent notificationIntent = new Intent(context, MainActivity.class); 
 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
 
 //Custom notification 
 NotificationCompat.Builder notification = new NotificationCompat.Builder(context) 
 .setContentTitle("Hello").setContentText(message).setSmallIcon(R.drawable.logo); 
 notification.setContentIntent(contentIntent); 
 
 //Dismiss notification when click on it 
 notification.setAutoCancel(true); 
 
 mNotificationManager.notify(NOTIFICATION_ID, notification.build()); 
 
 // super.onMessage(context, intent); 
 
 return false; 
 } 
}


Thanks for all the answers, it’s great to have these quick answers!

Best regards

Hi guys, I’ve been trying to do the same, but how do I call OnMessage method in my code??.. creating an instace of PushNotificationReceiver like this;

PushNotificationReceiver pnr=new PushNotificationReceiver();
pnr.OnMessage(getBaseContext,new Intent());
and where do I put this code.?.. inside HandleResponse in Backendless.Messaging.Publish(“default”,publisopcoins,deliveryoptios…) ?

Please Help
Thanks

Did you read the documentation at:
https://backendless.com/documentation/messaging/android/messaging_push_notification_setup_androi.htm

Specifically starting with the paragraph that begins with: “Custom handling of push notifications requires an implementation of two classes”

The main point is you do not call onMessage AT ALL. The method is invoked automatically when push notification arrives to the device.

Hi Mark, thanks for your response, I’ve made all what documentation says and push notifications works fine but I at the moment I click on push notification in the receiver device , it does not take me to my TargetActvity… this is my code of 2 classes.

PushService Class

public class PushService extends BackendlessPushService
{
@Override
public void onRegistered(Context context, String registrationId)
{
Toast.makeText( context, “device registered” + registrationId, Toast.LENGTH_SHORT).show();
}

@Override
public void onUnregistered( Context context, Boolean unregistered )
{
    Toast.makeText( context, "device unregistered", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onMessage( Context context, Intent intent)
{
 return false;

}

@Override
public void onError( Context context, String message )
{
    Toast.makeText( context, message, Toast.LENGTH_SHORT).show();
}

and this my PushReceiver class
public class MyPushReceiver extends BackendlessBroadcastReceiver
{
private static final int NOTIFICATION_ID = 1;
@Override
public boolean onMessage(Context context, Intent intent)
{
NotificationManager nManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.trebol_icon)
            .setContentTitle("Your question was answered")
            .setContentText("Message.")
            .setWhen(System.currentTimeMillis());

    Intent targetIntent = new Intent(context, TargetActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    builder.setAutoCancel(true);

    nManager.notify(NOTIFICATION_ID, builder.build());



    return true;
}

}and I’m a little confused if I must override onMessage Method from BroadcastReceiver or PushService.
Please helpRegards