//internal static bool
        void HandleNotification(string messageBody, string messageTitle)
        {
            var intent = new Intent(Forms.Context, typeof(MainActivity));

            intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
            var pendingIntent = PendingIntent.GetActivity(Forms.Context, 0, intent, PendingIntentFlags.UpdateCurrent);


            var n = new Notification.Builder(Forms.Context);

            n.SetSmallIcon(Resource.Drawable.notification_template_icon_bg);
            n.SetLights(Android.Graphics.Color.Blue, 300, 1000);
            n.SetContentIntent(pendingIntent);
            n.SetContentTitle(messageTitle);
            n.SetTicker(messageBody);
            //n.SetLargeIcon(global::Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.icon));
            n.SetAutoCancel(true);
            n.SetContentText(messageBody);
            n.SetVibrate(new long[] {
                200,
                200,
                100,
            });

            var nm = NotificationManager.FromContext(Forms.Context);

            nm.Notify(0, n.Build());

            // Make call to Xamarin.Forms here with the event handler
        }
        public void HandleNotification(string title, string msg)
        {
            var n = new Notification.Builder(Forms.Context);

            n.SetSmallIcon(Resource.Drawable.notification_template_icon_bg);
            n.SetLights(Android.Graphics.Color.Blue, 300, 1000);
            //n.SetContentIntent(pendingIntent);
            n.SetContentTitle(title);
            n.SetTicker(msg);
            //n.SetLargeIcon(global::Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.icon));
            n.SetAutoCancel(true);
            n.SetContentText(msg);
            n.SetVibrate(new long[] {
                200,
                200,
                100,
            });

            var nm = NotificationManager.FromContext(Forms.Context);

            nm.Notify(0, n.Build());
        }
예제 #3
0
        internal static bool HandlePushNotification(Context context, Intent intent)
        {
            string message;

            if (!intent.Extras.ContainsKey("message"))
            {
                return(false);
            }

            message = intent.Extras.Get("message").ToString();
            var title = intent.Extras.Get("title").ToString();

            var activityIntent = new Intent(context, typeof(MainActivity));
            var payloadValue   = GetPayload(intent);

            activityIntent.PutExtra("payload", payloadValue);
            activityIntent.SetFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(context, 0, activityIntent, PendingIntentFlags.UpdateCurrent);

            var n = new Notification.Builder(context);

            n.SetSmallIcon(Resource.Drawable.ic_successstatus);
            n.SetLights(global::Android.Graphics.Color.Blue, 300, 1000);
            n.SetContentIntent(pendingIntent);
            n.SetContentTitle(title);
            n.SetTicker(message);
            n.SetLargeIcon(global::Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.icon));
            n.SetSmallIcon(Resource.Drawable.ic_trophy_white);
            n.SetContentText(message);
            n.SetVibrate(new long[] {
                200,
                200,
                100,
            });

            var nm = NotificationManager.FromContext(context);

            nm.Notify(0, n.Build());

            if (MainActivity.IsRunning)
            {
                try
                {
                    message.ToToast();
                    var payload = GetPayload(intent);
                    var pl      = JsonConvert.DeserializeObject <NotificationPayload>(payload);

                    if (payloadValue != null)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            MessagingCenter.Send <App, NotificationPayload>(App.Current, Messages.IncomingPayloadReceivedInternal, pl);
                        });
                    }
                }
                catch (Exception e)
                {
                    InsightsManager.Report(e, Insights.Severity.Error);
                }
            }

            return(true);
        }
예제 #4
0
        static public void NotifyCommon(Context context, int id, bool replace,
                                        string tickerText, String title, String text, int icon,
                                        Intent notificationIntent, bool showLed, Uri soundUri, bool vibration, Uri contentUri)
        {
            PendingIntentFlags flags = 0;

            if (replace)
            {
                flags = PendingIntentFlags.CancelCurrent;
            }
            PendingIntent contentIntent = PendingIntent.GetActivity(context, id, notificationIntent, flags);

            NotificationDefaults defaults = 0;

            // If vibration was requested, use the system-default vibration pattern
            defaults = (vibration) ? NotificationDefaults.Vibrate : 0;
            // If notification light was requested, use system-default colour and blink pattern
            defaults |= (showLed) ? NotificationDefaults.Lights : 0;

            Notification.Builder notificationBuilder = new Notification.Builder(context)
                                                       .SetDefaults(defaults)
                                                       .SetSound(soundUri)
                                                       .SetContentTitle(title)
                                                       .SetTicker(tickerText)
                                                       .SetContentText(text)
                                                       .SetContentIntent(contentIntent)
                                                       .SetSmallIcon(icon, 0)
                                                       .SetAutoCancel(true);

            // No notification light if not requested
            if (!showLed)
            {
                notificationBuilder.SetLights(0, 0, 0);
            }

            // No vibration pattern if not requested
            if (!vibration)
            {
                notificationBuilder.SetVibrate(new long[] { 0, 0 });
            }

            Notification notification = null;

            if (contentUri != null)
            {
                try
                {
                    Notification.BigPictureStyle picBuilder = new Notification.BigPictureStyle(notificationBuilder)
                                                              .BigPicture(MediaStore.Images.Media.GetBitmap(context.ContentResolver, contentUri));
                    notification = picBuilder.Build();
                }
                catch (Exception e)
                {
                    Log.Error(LogTag, "Unable to decode incoming image for big notification: " + e);
                }
            }

            if (notification == null)
            {
                notification = notificationBuilder.Build();
            }

            NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);

            notificationManager.Notify(id, notification);
            Log.Warn(LogTag, title + ":" + text);
        }