void SendNotification(string message, string userId, string token, string userName)
        {
            var intent = new Intent(this, typeof(MainActivity));

            intent.PutExtra("userId", userId);
            intent.PutExtra("token", token);
            intent.PutExtra("userName", userName);
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);

            var notificationBuilder = new Notification.Builder(this)
                                      .SetSmallIcon(Resource.Drawable.Icon)
                                      .SetContentText(message)
                                      .SetStyle(new Notification.BigTextStyle().BigText(message))
                                      .SetAutoCancel(true)
                                      .SetContentIntent(pendingIntent);
            var isSound   = prefs.GetBoolean("IsSoundEnable", true);
            var isVibrate = prefs.GetBoolean("IsVibrateEnable", true);

            if (isSound)
            {
                notificationBuilder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
            }
            if (isVibrate)
            {
                notificationBuilder.SetVibrate(new long[] { 100, 400, 100, 500 });
            }
            var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);

            notificationManager.Notify(1, notificationBuilder.Build());
        }
        //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
        }
Exemple #3
0
        private void CheckServerNotifications(Context pContext, string sClan, string sName)
        {
            XElement pResponse = null;
            //try
            //{
            string sPrevClan = Master.GetActiveClan();
            string sPrevUser = Master.GetActiveUserName();

            Master.SetActiveClan(sClan);
            Master.SetActiveUserName(sName);
            string sResponse = WebCommunications.SendPostRequest(Master.GetBaseURL() + Master.GetServerURL() + "GetUnseenNotifications", Master.BuildCommonBody(), true);

            pResponse = Master.ReadResponse(sResponse);
            Master.SetActiveClan(sPrevClan);
            Master.SetActiveUserName(sPrevUser);
            //}
            //catch (Exception e) { return; }

            if (pResponse.Element("Data") != null && pResponse.Element("Data").Element("Notifications").Value != "")
            {
                List <XElement> pNotifications = pResponse.Element("Data").Element("Notifications").Elements("Notification").ToList();
                foreach (XElement pNotification in pNotifications)
                {
                    string sContent  = pNotification.Value;
                    string sGameID   = pNotification.Attribute("GameID").Value;
                    string sGameName = pNotification.Attribute("GameName").Value;

                    Notification.Builder pBuilder = new Notification.Builder(pContext);
                    pBuilder.SetContentTitle(sClan + " - " + sGameName);
                    pBuilder.SetContentText(sContent);
                    pBuilder.SetSmallIcon(Resource.Drawable.Icon);
                    pBuilder.SetVibrate(new long[] { 200, 50, 200, 50 });
                    pBuilder.SetVisibility(NotificationVisibility.Public);
                    pBuilder.SetPriority((int)NotificationPriority.Default);

                    Intent pIntent = null;
                    if (sGameID.Contains("Zendo"))
                    {
                        pIntent = new Intent(pContext, typeof(ZendoActivity));
                    }
                    pIntent.SetAction(sGameID);
                    pIntent.PutExtra("GameName", sGameName);

                    pBuilder.SetContentIntent(PendingIntent.GetActivity(pContext, 0, pIntent, 0));
                    pBuilder.SetStyle(new Notification.BigTextStyle().BigText(sContent));

                    Notification        pNotif   = pBuilder.Build();
                    NotificationManager pManager = (NotificationManager)pContext.GetSystemService(Context.NotificationService);
                    pManager.Notify((int)Java.Lang.JavaSystem.CurrentTimeMillis(), pNotif);                     // using time to make different ID every time, so doesn't replace old notification
                    //pManager.Notify(DateTime.Now.Millisecond, pNotif); // using time to make different ID every time, so doesn't replace old notification
                }
            }
        }
Exemple #4
0
        protected override void OnCreate(Bundle bundle)
        {
            int i = 1;

            base.OnCreate(bundle);
            SetContentView(Resource.Layout.PaymentPage);
            Button paymentbtn = FindViewById <Button>(Resource.Id.paymentbtn);

            paymentbtn.Click += (o, e) =>
            {
                Toast.MakeText(this, "Confirmed", ToastLength.Short).Show();


                Notification.BigTextStyle textStyle = new Notification.BigTextStyle();

                textStyle.BigText("Your Code is 1234");
                // Set the summary text:
                textStyle.SetSummaryText("Enjoy :)");

                var notificationBuilder = new Notification.Builder(this)

                                          .SetContentTitle("Smarty's Notification")
                                          .SetContentText("Body goes here...........This is a big Notificaiton in Notification Tray")
                                          .SetAutoCancel(true)

                                          .SetPriority((int)NotificationPriority.Max)
                                          .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate | NotificationDefaults.Lights)
                                          .SetStyle(textStyle);

                if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    notificationBuilder.SetVisibility(NotificationVisibility.Public);
                    notificationBuilder.SetCategory(Notification.CategoryAlarm);
                    //notificationBuilder.SetSmallIcon(Resource.Drawable.transparentNotification);
                    notificationBuilder.SetSmallIcon(Resource.Drawable.logo);
                    //notificationBuilder.SetColor(Resources.GetColor(Resource));
                    notificationBuilder.SetVibrate(new long[] { 100, 200, 300 });
                }
                else
                {
                    notificationBuilder.SetSmallIcon(Resource.Drawable.logo);
                }

                var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notificationManager.Notify(i++, notificationBuilder.Build());
            };
        }
        /// <summary>
        /// Show a local notification
        /// </summary>
        /// <param name="title">Title of the notification</param>
        /// <param name="body">Body or description of the notification</param>
        /// <param name="id">Id of the notification</param>
        public void Show(string title, string body, int id = 0)
        {
            var builder = new Notification.Builder(Application.Context);

            //var builder = new NotificationCompat.Builder(Application.Context);

            builder.SetContentTitle(title);
            builder.SetContentText(body);
            builder.SetAutoCancel(true);
            builder.SetPriority(Priority);
            builder.SetVibrate(Vibrate);


            if (NotificationIconId != 0)
            {
                builder.SetSmallIcon(NotificationIconId);
            }
            else
            {
                builder.SetSmallIcon(Resource.Drawable.plugin_lc_smallicon);
            }

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                var channelId = $"{_packageName}.general";
                //var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default);
                var channel = new NotificationChannel(channelId, "General", Importance);

                _manager.CreateNotificationChannel(channel);

                builder.SetChannelId(channelId);
            }

            var resultIntent = GetLauncherActivity();

            resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
            var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context);

            stackBuilder.AddNextIntent(resultIntent);
            var resultPendingIntent =
                stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            builder.SetContentIntent(resultPendingIntent);

            _manager.Notify(id, builder.Build());
        }
Exemple #6
0
        void SendNotification(MessaggioNotifica m)
        {
            var intent = new Intent (this, typeof(MainActivity));

            intent.AddFlags (ActivityFlags.ClearTop);

            var notifica = new Notification.Builder (this);
            switch (m.Tipo) {
            case "1":
                notifica.SetSmallIcon (Resource.Drawable.alert_1);
                break;
            case "2":
                notifica.SetSmallIcon (Resource.Drawable.alert_2);
                break;
            case "3":
                notifica.SetSmallIcon (Resource.Drawable.alert_3);
                break;
            default:
                notifica.SetSmallIcon (Resource.Drawable.InvioDoc);
                break;
            }

            if (m.Tipo == "4") {
                Messaggio mess = new Messaggio (m.Titolo, m.Messaggio);
                intent.PutExtra ("messaggio", JsonConvert.SerializeObject(mess));
            } else {
                intent.PutExtra ("commessanum", m.Commessa);
            }
            var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);

            notifica.SetContentTitle (m.Titolo);
            notifica.SetContentText (m.Messaggio);
            notifica.SetAutoCancel (true);
            notifica.SetContentInfo (m.Commessa);
            notifica.SetSubText ("Commessa " + m.Commessa);
            notifica.SetVibrate (new long[] {100,200,100});
            notifica.SetSound (RingtoneManager.GetDefaultUri(RingtoneType.Notification));
            notifica.SetContentIntent (pendingIntent);
            var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
            notificationManager.Notify (0, notifica.Build());
        }
        public void IssueNotificationAsync(string title, string message, bool autoCancel, bool ongoing, string id, bool playSound, bool vibrate)
        {
            if (_notificationManager != null)
            {
                new Thread(() =>
                {
                    if (message == null)
                    {
                        _notificationManager.Cancel(id, 0);
                    }
                    else
                    {
                        Intent serviceIntent = new Intent(_service, typeof(AndroidSensusService));
                        serviceIntent.PutExtra(NOTIFICATION_ID_KEY, id);
                        PendingIntent pendingIntent = PendingIntent.GetService(_service, 0, serviceIntent, PendingIntentFlags.UpdateCurrent);

                        Notification.Builder builder = new Notification.Builder(_service)
                                                       .SetContentTitle(title)
                                                       .SetContentText(message)
                                                       .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                       .SetContentIntent(pendingIntent)
                                                       .SetAutoCancel(autoCancel)
                                                       .SetOngoing(ongoing);

                        if (playSound)
                        {
                            builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
                        }

                        if (vibrate)
                        {
                            builder.SetVibrate(new long[] { 0, 250, 50, 250 });
                        }

                        _notificationManager.Notify(id, 0, builder.Build());
                    }
                }).Start();
            }
        }
        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());
        }
        /// <summary>
        /// Issues the notification.
        /// </summary>
        /// <param name="title">Title.</param>
        /// <param name="message">Message.</param>
        /// <param name="id">Identifier of notification.</param>
        /// <param name="protocolId">Protocol identifier to check for alert exclusion time windows.</param>
        /// <param name="alertUser">If set to <c>true</c> alert user.</param>
        /// <param name="displayPage">Display page.</param>
        public override void IssueNotificationAsync(string title, string message, string id, string protocolId, bool alertUser, DisplayPage displayPage)
        {
            if (_notificationManager == null)
            {
                return;
            }

            Task.Run(() =>
            {
                if (message == null)
                {
                    CancelNotification(id);
                }
                else
                {
                    Intent notificationIntent = new Intent(_service, typeof(AndroidSensusService));
                    notificationIntent.PutExtra(DISPLAY_PAGE_KEY, displayPage.ToString());

                    PendingIntent notificationPendingIntent = PendingIntent.GetService(_service, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);

                    Notification.Builder notificationBuilder = new Notification.Builder(_service)
                                                               .SetContentTitle(title)
                                                               .SetContentText(message)
                                                               .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                               .SetContentIntent(notificationPendingIntent)
                                                               .SetAutoCancel(true)
                                                               .SetOngoing(false);

                    if (alertUser && !Protocol.TimeIsWithinAlertExclusionWindow(protocolId, DateTime.Now.TimeOfDay))
                    {
                        notificationBuilder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
                        notificationBuilder.SetVibrate(new long[] { 0, 250, 50, 250 });
                    }

                    _notificationManager.Notify(id, 0, notificationBuilder.Build());
                }
            });
        }
Exemple #10
0
        public Notification.Builder CreateNotificationBuilder(SensusNotificationChannel channel)
        {
            global::Android.Net.Uri notificationSoundURI = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

            AudioAttributes notificationAudioAttributes = new AudioAttributes.Builder()
                                                          .SetContentType(AudioContentType.Unknown)
                                                          .SetUsage(AudioUsageKind.NotificationEvent).Build();

            long[] vibrationPattern = { 0, 250, 50, 250 };

            bool silent = GetChannelSilent(channel);

            Notification.Builder builder;

            // see the Backwards Compatibility article for more information
#if __ANDROID_26__
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                NotificationManager notificationManager = Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager;

                string channelId = channel.ToString();

                if (notificationManager.GetNotificationChannel(channelId) == null)
                {
                    NotificationChannel notificationChannel = new NotificationChannel(channelId, GetChannelName(channel), GetChannelImportance(channel))
                    {
                        Description = GetChannelDescription(channel)
                    };

                    if (silent)
                    {
                        notificationChannel.SetSound(null, null);
                        notificationChannel.EnableVibration(false);
                    }
                    else
                    {
                        notificationChannel.SetSound(notificationSoundURI, notificationAudioAttributes);
                        notificationChannel.EnableVibration(true);
                        notificationChannel.SetVibrationPattern(vibrationPattern);
                    }

                    notificationManager.CreateNotificationChannel(notificationChannel);
                }

                builder = new Notification.Builder(Application.Context, channelId);
            }
            else
#endif
            {
#pragma warning disable 618
                builder = new Notification.Builder(Application.Context);

                if (silent)
                {
                    builder.SetSound(null);
                    builder.SetVibrate(null);
                }
                else
                {
                    builder.SetSound(notificationSoundURI, notificationAudioAttributes);
                    builder.SetVibrate(vibrationPattern);
                }
#pragma warning restore 618
            }

            return(builder);
        }
        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);
        }
Exemple #12
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);
        }