Push Notification with AppCenter when the app is closed

EnricoRossiniEnricoRossini USMember ✭✭✭✭

Hi guys,
I'm implementing Push Notification in my app with AppCenter. In UWP I created a background service to receive message when the app is closed. I'm trying to do the same with Android and, later, iOS.

In Android project I created a IntentService to initialize the Push from AppCenter but it doesn't work.

[Service]
public class PushNotificationService : IntentService
{
    public override void OnCreate()
    {
       if (!AppCenter.Configured)
        {
            Push.PushNotificationReceived += (sender, e) =>
            {
                new NotificationHelpers().ShowNotification(e.Title, e.Message, e.CustomData);
            };
        }
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        return StartCommandResult.StickyCompatibility;
    }

    protected override void OnHandleIntent(Intent intent)
    {
    }
}

What is the correct implementation for Android? Thank you

Answers

  • MommMomm USMember ✭✭✭
    edited February 9

    Have you registered the tokens, etc?

    Also, shouldn't it be AppCenter.Configured instead of !AppCenter.Configured?

  • EnricoRossiniEnricoRossini USMember ✭✭✭✭

    @Momm said:
    Have you registered the tokens, etc?

    Also, shouldn't it be AppCenter.Configured instead of !AppCenter.Configured?

    I can receive with this code a Push Notification when the app is open. I copied this code in a service as I wrote in my post but it doesn't work.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    You should not need a service to handle push notifications on Android or iOS. That's the point of them. If you configure your app correctly, the OS will display the notification and bring the app to the foreground (even if it's not running when the Push Notification arrives).

    Unless I'm missing something regarding how App Center implements push notifications (the last time I looked it used Firebase Cloud Messaging).
  • EnricoRossiniEnricoRossini USMember ✭✭✭✭

    @JamesLavery thank you for your comment. The implementation of Push Notification cross platform with AppCenter is very easy as they explain in this document. But a simple test shows that it is not working when the app is close. For this reason I created a background service. Happy to change.

  • EricBowmanEricBowman USMember ✭✭

    I'm having the same problem and I'm looking into it as well. Did you ever find a solution?

  • Angelru9Angelru9 ESMember ✭✭✭
    edited April 26

    @EnricoRossini @EricBowman I think that with appcenter it's not possible...

    https://docs.microsoft.com/en-us/appcenter/sdk/push/android

     Note
    
    A notification is not generated when your application receives a push in the foreground.
    
     Note
    
    If the push is received in background, the event is NOT triggered at receive time. The event is triggered when you click on the notification.
    
     Note
    
    The background notification click callback does NOT expose title and message. Title and message are only available in foreground pushes.
    

    With Firebase Cloud Messaging it works, in my app with the next plugin:

    https://github.com/CrossGeeks/FirebasePushNotificationPlugin

  • RicardoBritoBrensRicardoBritoBrens USMember ✭✭

    Hello EnricoRossini

    I was asking me the same question for a while and a i've just implemented push notificaction using AppCenter and FireBase in a Xamarin Forms just for android and those link's below gave what a needed.

    This one is an explanation for ios

    This one is an explanation for xamarin forms
    https://ozaksut.com/appcenter-push-notification-part-1/

    I hope this help you as did for me. :)

  • EnricoRossiniEnricoRossini USMember ✭✭✭✭

    Thank you @RicardoBritoBrens

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    My problem I am not receiving the push notification when the app is in foreground and also when the app was closed / terminated (both for android)

    I am only receiving when the app is running in the background

    how can i fix this please..

Sign In or Register to comment.