0

Hi I am using Push Notification Plugin for Xamarin for implementing push notification in my app and I am receiving push notifications (Using GCM) but the problem is the push notifications I sent to the device are being replaced when I sent a new one and that is not the expected behavior, I want to show all the notifications received, This is how I configured my android app

public override void OnCreate()
    {
        base.OnCreate();
        AppContext = this.ApplicationContext;
        CrossPushNotification.NotificationContentTextKey = "message";
        CrossPushNotification.NotificationContentTitleKey = "contentTitle";
        CrossPushNotification.NotificationContentDataKey = "data";
        CrossPushNotification.Initialize<CrossPushNotificationListener>("MYANDROIDSENDERID");
        //This service will keep your app receiving push even when closed.             
        CrossPushNotification.Current.Register();
        StartPushService();
        RegisterActivityLifecycleCallbacks(this);

    }
    public static void StartPushService()
    {
        AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));

        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
        {
            Random rnd = new Random();
            // Publish the notification:
            PendingIntent pintent = PendingIntent.GetService(AppContext,0, new Intent(AppContext, typeof(PushNotificationService)),0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            alarm.Cancel(pintent);
        }
    }

What am I missing ?

Vishnu Babu
  • 1,183
  • 1
  • 13
  • 37
  • You receive one push notification then send a second one, or you send two the same time to GCM? Also tell me, do you open the first notification and what exactly do you want to keep from previous notification? – Emad Feb 27 '17 at 07:48
  • I receive one push notification and I do nothing on it,then I send the second one after some time instead of displaying both the notification I can only see the last one sent to the device.. – Vishnu Babu Feb 27 '17 at 08:53

2 Answers2

0

This is the design pattern from google (and I've heard that the same applies to iOS) therefore you can have multiple notifications from native code but your app will be rejected from Play Store and is not following Google's pattern therefore doing this is not possible in Push Notification Plugin written for Xamarin.

For more info see this and this

Community
  • 1
  • 1
Emad
  • 3,809
  • 3
  • 32
  • 44
0

Actually this was possible, I had to go platform specific to achieve this. Instead of the CrossPushNotificationListener which was defined in the PCL that implemented IPushNotificationListener interface, I created Another class in the droid project and implemented the IPushNotificationListener interface and then used it to initialize pushnotification then with in OnMessage method I added code to generate notification with Notification.Builder as follows

void IPushNotificationListener.OnMessage(JObject parameters, DeviceType deviceType)
    {
        try
        {

            Intent intent = new Intent(Android.App.Application.Context, typeof(MainActivity));
            Intent[] intar = { intent };
            PendingIntent pintent = PendingIntent.GetActivities(Android.App.Application.Context, 0, intar, 0);
            intent.SetAction("notification");
            //Json Response Recieved 
            Dictionary<string, string> results = JsonConvert.DeserializeObject<Dictionary<string, string>>(parameters.ToString());
            Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)
     .SetContentTitle(results["contentTitle"])
     .SetContentText(results["message"])
     .SetContentIntent(pintent)
     .SetSmallIcon(Resource.Drawable.icon);
            // Build the notification:
            Notification notification = builder.Build();

            //Clear notification on click
            notification.Flags = NotificationFlags.AutoCancel;

            // Get the notification manager:
            NotificationManager notificationManager =
                Android.App.Application.Context.GetSystemService(PushNotificationService.NotificationService) as NotificationManager;
           //notificationId  need to be unique for each message same ID will update existing message
            int notificationId = Convert.ToInt32(results["Id"]);
            // Publish the notification:
            notificationManager.Notify(notificationId, notification);
        }
        catch (Exception)
        {
        }
    }
Vishnu Babu
  • 1,183
  • 1
  • 13
  • 37