Change push notification text in android

Viewed 711

I have created a xamarin app, and I want to edit the text of a push notification text before it gets displayed to the user.

I am using a Notification Extenstion Service and also OneSingal library to get the notifications.

using System;
using Android.App;
using Android.Content;
using Android.Preferences;
using Android.Support.V4.App;
using Com.OneSignal.Android;
using MyApp.Shared.Utility;

namespace MyApp.Droid
{
[Service(Permission = "android.permission.BIND_JOB_SERVICE")]
[IntentFilter(new String[] { "com.onesignal.NotificationExtender" })]
public class CustomNotificationExtenderService : NotificationExtenderService
{
    private OSNotificationReceivedResult _result;
    private NotificationManager notifyManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
    private NotificationCompat.Builder builder = new NotificationCompat.Builder(Application.Context, "1");
    protected override void OnHandleIntent(Intent intent){ }
    protected override bool OnNotificationProcessing(OSNotificationReceivedResult p0)
    {
        _result = p0;
        var body = "New Encrypted Message";

        NotificationCompat.Builder builder = new NotificationCompat.Builder(Application.Context, "1");
        
        builder.SetSmallIcon(Resource.Drawable.Logo);
        builder.SetStyle(new NotificationCompat.BigTextStyle().BigText(body));
        builder.SetContentTitle("New Message");
        builder.SetContentText(body);
        // Build the notification and display it
        notifyManager.Notify(1, builder.Build());
        return false;
    }
}
}

Please note that in my sample code above I have omitted the logic for the creation of text that goes in the notification banner. I was expecting to see a push Notification with a text 'New Encrypted Message'. But it shows the actual message that sends from the server.

I would appreciate your help

2 Answers

If need to show custom message, you could have a try with overriding a NotificationCompat.Builder and return it to OverrideSettings.

Sample code as follow:

import android.support.v4.app.NotificationCompat;
import com.onesignal.OSNotificationDisplayedResult;
import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationReceivedResult;
import java.math.BigInteger;
import android.util.Log;

public class NotificationExtenderExample extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      OverrideSettings overrideSettings = new OverrideSettings();
      overrideSettings.extender = new NotificationCompat.Extender() {
         @Override
         public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
            //Force remove push from Notification Center after 30 seconds
            builder.setTimeoutAfter(30000);
            // Sets the icon accent color notification color to Green on Android 5.0+ devices.
            builder.setColor(new BigInteger("FF00FF00", 16).intValue());
            builder.setContentTitle("New Message");
            builder.setContentText("New Encrypted Message");
            return builder;
         }
      };

      OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
      Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);

      // Return true to stop the notification from displaying.
      return false;
   }
}

More info can refer to OneSignal Service Extensions document.

Every notification comes in 2 way. 1 is application is in foreground and other is application is in background. If app is in background than notification is handled by itself either by Google or iOS.

If your code is not executing it means you are checking for background notification.

If you want that your code must be executed in all the way either background or foreground than you should use silent push notification.

For push notification you have to do nothing. It is backend side code. In backend they have to send payload as 'data' => $data instead of 'notification' => $data and for ios 1 extra thing need to add is 'content-available' => '1'

For more reference you can check this link which is for php backend. Syntax may be different for other backend server.

Related