OneSignal / OneSignal-Xamarin-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Xamarin app with OneSignal. https://onesignal.com
Other
105 stars 50 forks source link

Android HandleNotificationOpened Or HandleNotificationReceived #110

Closed ketanparekh closed 4 years ago

ketanparekh commented 6 years ago

Looked both #49 #86

I am working with Xamarin.Forms Onesignal SDK and I am able to get Push Notification in iOS and Android both. I want to get additional data when android application is not running and user open push notification.

Scenarion :

Version Information :

Thanks, Ketan

jkasten2 commented 6 years ago

@ketanparekh HandleNotificationOpened should be called in this case. Can you share your OneSignal code?

ketanparekh commented 6 years ago

@jkasten2 Thanks for the reply. Below is the code for 'OneSignal'.

Initial in public App()

OneSignal.Current.StartInit("xxxxxx")
                     .HandleNotificationReceived(HandleNotificationReceived)
                     .HandleNotificationOpened(HandleNotificationOpened)
                     .EndInit();
 private static void HandleNotificationReceived(OSNotification notification)
{
            OSNotificationPayload payload = notification.payload;
            string message = payload.body;
            Dictionary<string, object> additionalData = payload.additionalData; 

            if (additionalData != null)
            {
                if (additionalData.ContainsKey("screenName"))
                {
                    pushScreenName = Convert.ToString(additionalData["screenName"]);
                }
                else
                {
                    pushScreenName = "";
                }
            }
}
private static void HandleNotificationOpened(OSNotificationOpenedResult result)
{
            OSNotificationPayload payload = result.notification.payload;
            Dictionary<string, object> additionalData = payload.additionalData;
            string message = payload.body;
            string actionID = result.action.actionID;

            if (additionalData != null)
            {
                if (additionalData.ContainsKey("screenName"))
                {
                    pushScreenName = Convert.ToString(additionalData["screenName"]);
                }
                else
                {
                    pushScreenName = "";
                }
            }
}
dinonovak commented 6 years ago

HI all, I am experiencing similar behaviour on Android.

Scenario : 1) Android application is not running.

2) Android application is running in background.

Details: Xamarin.Forms 3.1.0.697729 VS mac 7.5.4 (build 3) OneSignal 3.2.0 Min Android Version: API level 21 Target Android Version: API level 26 Target Framework: 8.1

Actual device is Android 7.0 device

here is the code `private async static void HandleNotificationOpened(OSNotificationOpenedResult result) { OSNotificationPayload payload = result.notification.payload; string message = payload.body;

        MessageItem newMessage = new MessageItem();
        newMessage.Subject = payload.title;
        newMessage.Body = payload.body;
        newMessage.TimeSent = DateTime.Now;
        await App.Database.SaveItemAsync(newMessage);

        if(payload.additionalData.Count > 0)
        {

            //check to see what we need to do
            Dictionary<string, object> toDo = payload.additionalData;

            Object screenVal;
            toDo.TryGetValue("OpenScreen", out screenVal);

            String strScreen = screenVal.ToString();

            // find which screen id we need to use
            Helper clsHelper = new Helper();
            ScreenDef gotoScreen = clsHelper.GetScreenID(GlobalVars.XMLScreens, strScreen);
            if (gotoScreen.ScreenID != -1)
            {
                if (gotoScreen.ScreenType == "item_description")

                    await App.Current.MainPage.Navigation.PushModalAsync(new pageDetails(gotoScreen.ScreenID));    
            }
            //}

        }
    }`

On iOS the same code is working without any issues. Also in Android Emulator (API 24) behaviour is the same.

jBijsterboschNL commented 6 years ago

@ketanparekh and @dinonovak have you guys checked this? https://github.com/OneSignal/OneSignal-Xamarin-SDK/issues/48

rgomezp commented 5 years ago

Closing due to no response

hugodantas commented 5 years ago

I have the same issue here... On method HandleNotificationReceived I have custom behavior, like saving the notification in a local LiteDB Database, and the method is NOT called when notification is received.

The notification is received when app is closed (or terminated by swipe on recent apps screen on Android) but no handle methods are called.

@jkasten2 can you please reopen this issue for investigation? Thank you so much!

MoonAndorid7 commented 4 years ago

I have the same issue here... @rgomezp Please suggest

rgomezp commented 4 years ago

Please take another look at our documentation regarding background notifications and make sure you have followed the instructions carefully and understand the limitations.

MoonAndorid7 commented 4 years ago

@rgomezp Yes, I have checked this document and I have followed all its instructions. And I am not forced closing the application. I am simply swipe closing it.

it is working well when the application is in the background. HandleNotificationOpened is not calling when the application is not in the background. for ios, it is working fine altho

rgomezp commented 4 years ago

@MoonAndorid7 , It sounds like you're talking about notifications that arrive while the app is in focus and then are tapped, correct? You followed the example here?

If the app is in focus, you can use HandleNotificationReceived

rgomezp commented 4 years ago

Closing due to no response.