相关文章推荐
面冷心慈的松球  ·  java ...·  1 年前    · 
听话的香菜  ·  Laravel 10 - PHP 8.2 ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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:

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

  • 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()
    
  • 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

  • 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?

    May be the notifications disabled for your application (You can check it in settings of iOS). – Andrew Romanov Oct 28, 2019 at 4:07 @AndrewRomanov: I get the notification coming through to the app, but it does not show the Badge or the new contents that I am changing in the DidReceiveNotificationRequest method as part of test – Libin Joseph Oct 28, 2019 at 4:10 What's the deployment target of your extension? Make sure it is lower than the device system version which you are testing. – nevermore Oct 28, 2019 at 8:10 I now have the deployment target for the ios app set to 10.3 and notification extension to 10.2 But it still does not work. Anything else that I can check? – Libin Joseph Oct 28, 2019 at 22:34 Can you add a breakpoint there to check if the code triggered? Here is also an official sample you can refer. – nevermore Oct 29, 2019 at 3:17

    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?

    Turns out that i am getting the bade count working on ios 13, but not on 11 and 12. All these while I was testing on 12. ANy idea? – Libin Joseph Nov 5, 2019 at 3:22 @LibinJoseph also apple wrote in documentation: "Note - You cannot modify silent notifications or those that only play a sound or badge the app’s icon." – Mark Nov 5, 2019 at 19:28 @LibinJoseph The user may change the allowed interactions at any time in system settings. Use the getNotificationSettings(completionHandler:) method to determine what interactions are currently allowed for your app. (in your main app appDelegate) Also you could check right in iPhone settings for your app.(stackoverflow.com/a/24990541/9244718) – Mark Nov 5, 2019 at 19:47

    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()
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.