Customise push notification

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