Libin Joseph
Libin Joseph

Reputation: 7362

DidReceiveNotificationRequest in not getting called

I have a xamarin forms application and ios Notification service extension is not getting called when I receive notification from server.

I have done the following things so far:

  1. Have added the mutable-content = 1 in the apns payload.

  2. This is how I manipulate the apns payload in the service

    public class NotificationService : UNNotificationServiceExtension
    {
        Action<UNNotificationContent> ContentHandler { get; set; }
        UNMutableNotificationContent BestAttemptContent { get; set; }

        protected NotificationService(IntPtr handle) : base(handle)
        {

        }

        public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
        {
            ContentHandler = contentHandler;
            BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();

            var newAlertContent = new UNMutableNotificationContent
            {
                Body = "Body from Service",
                Title = "Title from Service",
                Sound = BestAttemptContent.Sound,
                Badge = 2
            };
            ContentHandler(newAlertContent);
        }

        public override void TimeWillExpire()
        {
        }
    }
  1. I also have the the notification service extension bundle id done.(My app bundle id is com.companyname.appname.test and the extension bundle id is com.companyname.appname.test.xxxxServiceExtension

  2. In the AppDelegate class in Finishlaunching method I also have the permission code added.

  UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => {
            });

Is there anything else that I need to do?

Upvotes: 3

Views: 2487

Answers (2)

jakerl
jakerl

Reputation: 108

i saw something similar that drove me crazy a while back. i can't tell if what you're describing is what I was experiencing but i do remember the fix. i don't speak Xamarin.Forms, so best i can do is post the swift code, but i had to re register for notifications every time the app launched

func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

   // all your other config

   UIApplication.shared.registerForRemoteNotifications()
}

Upvotes: 0

Mark
Mark

Reputation: 711

The system executes your notification content app extension only when a remote notification’s payload contains the following information:

The payload must include the mutable-content key with a value of 1.

The payload must include an alert dictionary with title, subtitle, or body information.

Listing 2 shows the JSON data for a notification payload containing encrypted data. The mutable-content flag is set so that the user’s device knows to run the corresponding service app extension, the code for which is shown in .

Listing 2 Specifying the remote notification payload

{
   “aps” : {
      “category” : “SECRET”,
      “mutable-content” : 1,
      “alert” : {
         “title” : “Secret Message!”,
         “body”  : “(Encrypted)”
     },
   },
   “ENCRYPTED_DATA” : “Salted__·öîQÊ$UDì_¶Ù∞è   Ω^¬%gq∞NÿÒQùw”
}

Are you sure that you specify alert key?

Upvotes: 2

Related Questions