/// <summary>
        /// Attach this method to <see cref="ParsePush.ParsePushNotificationReceived"/> to utilize a
        /// default handler for push notification.
        /// </summary>
        /// <remarks>
        /// This handler will try to get the launcher <see cref="Activity"/> and application icon, then construct a
        /// <see cref="Notification"/> out of them. It uses push payload's <c>title</c> and <c>alert</c> as the
        /// <see cref="Notification.ContentView"/> title and text.
        /// </remarks>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void DefaultParsePushNotificationReceivedHandler(object sender, AVPushNotificationEventArgs args)
        {
            IDictionary<string, object> pushData = args.Payload;
              Context context = Application.Context;

              if (pushData == null || (!pushData.ContainsKey("alert") && !pushData.ContainsKey("title"))) {
            return;
              }

              string title = pushData.ContainsKey("title") ? pushData["title"] as string : ManifestInfo.DisplayName;
              string alert = pushData.ContainsKey("alert") ? pushData["alert"] as string : "Notification received.";
              string tickerText = title + ": " + alert;

              Random random = new Random();
              int contentIntentRequestCode = random.Next();

              Intent activityIntent = ManifestInfo.LauncherIntent;
              PendingIntent pContentIntent = PendingIntent.GetActivity(context, contentIntentRequestCode, activityIntent, PendingIntentFlags.UpdateCurrent);

              NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .SetContentTitle(new Java.Lang.String(title))
            .SetContentText(new Java.Lang.String(alert))
            .SetTicker(new Java.Lang.String(tickerText))
            .SetSmallIcon(ManifestInfo.PushIconId)
            .SetContentIntent(pContentIntent)
            .SetAutoCancel(true)
            .SetDefaults(NotificationDefaults.All);

              Notification notification = builder.Build();
              NotificationManager manager = context.GetSystemService(Context.NotificationService) as NotificationManager;
              int notificationId = (int)DateTime.UtcNow.Ticks;

              try {
            manager.Notify(notificationId, notification);
              } catch (Exception) {
            // Some phones throw exception for unapproved vibration.
            notification.Defaults = NotificationDefaults.Lights | NotificationDefaults.Sound;
            manager.Notify(notificationId, notification);
              }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attach this method to <see cref="ParsePush.ParsePushNotificationReceived"/> to utilize a
        /// default handler for push notification.
        /// </summary>
        /// <remarks>
        /// This handler will try to get the launcher <see cref="Activity"/> and application icon, then construct a
        /// <see cref="Notification"/> out of them. It uses push payload's <c>title</c> and <c>alert</c> as the
        /// <see cref="Notification.ContentView"/> title and text.
        /// </remarks>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void DefaultParsePushNotificationReceivedHandler(object sender, AVPushNotificationEventArgs args)
        {
            IDictionary <string, object> pushData = args.Payload;
            Context context = Application.Context;

            if (pushData == null || (!pushData.ContainsKey("alert") && !pushData.ContainsKey("title")))
            {
                return;
            }

            string title      = pushData.ContainsKey("title") ? pushData["title"] as string : ManifestInfo.DisplayName;
            string alert      = pushData.ContainsKey("alert") ? pushData["alert"] as string : "Notification received.";
            string tickerText = title + ": " + alert;

            Random random = new Random();
            int    contentIntentRequestCode = random.Next();

            Intent        activityIntent = ManifestInfo.LauncherIntent;
            PendingIntent pContentIntent = PendingIntent.GetActivity(context, contentIntentRequestCode, activityIntent, PendingIntentFlags.UpdateCurrent);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                                 .SetContentTitle(new Java.Lang.String(title))
                                                 .SetContentText(new Java.Lang.String(alert))
                                                 .SetTicker(new Java.Lang.String(tickerText))
                                                 .SetSmallIcon(ManifestInfo.PushIconId)
                                                 .SetContentIntent(pContentIntent)
                                                 .SetAutoCancel(true)
                                                 .SetDefaults(NotificationDefaults.All);

            Notification        notification = builder.Build();
            NotificationManager manager      = context.GetSystemService(Context.NotificationService) as NotificationManager;
            int notificationId = (int)DateTime.UtcNow.Ticks;

            try {
                manager.Notify(notificationId, notification);
            } catch (Exception) {
                // Some phones throw exception for unapproved vibration.
                notification.Defaults = NotificationDefaults.Lights | NotificationDefaults.Sound;
                manager.Notify(notificationId, notification);
            }
        }