Example #1
0
        public AppleNotification()
        {
            DeviceToken = string.Empty;
            Payload = new AppleNotificationPayload();

            Identifier = GetNextIdentifier();
        }
Example #2
0
        static void Main(string[] args)
        {
            //Create our push services broker
            var push = new PushBroker();
            var cert = File.ReadAllBytes(@"E:\Projects\PushMessage\Ysd.ApplicationInterface\App_Data\ApplePushCertDev.p12");
            push.RegisterAppleService(new ApplePushChannelSettings(false, cert, "your password"));

            var payload = new AppleNotificationPayload()
            {
                Badge = 7,
                Alert = new AppleNotificationAlert()
                {
                    Body = "Hello " + Guid.NewGuid(),
                    Title = "my title",
                    LocalizedArgs = new List<object> { "Jenna", "Frank" }
                },
                Sound = "chime.aiff"
            };

            push.QueueNotification(
                new AppleNotification("74b14f0ff695f71bb0a34925aada53f6eadcfe824bf461959403851e5f85b43d", payload));

            Console.WriteLine("Queue Finished, press return to exit...");
            Console.ReadLine();
        }
Example #3
0
        public AppleNotification()
        {
            DeviceToken = string.Empty;
            Payload     = new AppleNotificationPayload();

            Identifier = GetNextIdentifier();
        }
Example #4
0
        public AppleNotification(string deviceToken, AppleNotificationPayload payload)
        {
            DeviceToken = new DeviceToken(deviceToken);
            Payload = payload;

            Identifier = GetNextIdentifier();
        }
        public static AppleNotification WithPasskitUpdate(this AppleNotification n)
        {
            AppleNotificationPayload notificationPayload = new AppleNotificationPayload();

            notificationPayload.AddCustom("aps", (object)string.Empty);
            n.Payload = notificationPayload;
            return(n);
        }
Example #6
0
        public AppleNotification(DeviceToken deviceToken)
        {
            DeviceToken = deviceToken;

            Payload = new AppleNotificationPayload();

            Identifier = GetNextIdentifier();
        }
Example #7
0
        public AppleNotification()
        {
            this.Platform = Common.PlatformType.Apple;

            Payload = new AppleNotificationPayload();

            Identifier = GetNextIdentifier();
        }
Example #8
0
        public AppleNotification()
        {
            this.Platform = Common.PlatformType.Apple;

            DeviceToken = string.Empty;
            Payload = new AppleNotificationPayload();

            Identifier = GetNextIdentifier();
        }
Example #9
0
        public AppleNotification()
        {
            this.Platform = Common.PlatformType.Apple;

            DeviceToken = string.Empty;
            Payload     = new AppleNotificationPayload();

            Identifier = GetNextIdentifier();
        }
Example #10
0
        public AppleNotification(string deviceToken, AppleNotificationPayload payload)
        {
            if (!string.IsNullOrEmpty(deviceToken) && deviceToken.Length != DEVICE_TOKEN_STRING_SIZE)
                throw new NotificationFailureException(5, this); //Invalid DeviceToken Length

            DeviceToken = deviceToken;
            Payload = payload;

            Identifier = GetNextIdentifier();
        }
 public AppleNotification(string deviceToken, AppleNotificationPayload payload)
 {
     if (!string.IsNullOrEmpty(deviceToken) && deviceToken.Length != 64)
     {
         throw new NotificationFailureException(5, this);
     }
     this.DeviceToken = deviceToken;
     this.Payload     = payload;
     this.Identifier  = AppleNotification.GetNextIdentifier();
 }
Example #12
0
        public AppleNotification(string deviceToken, AppleNotificationPayload payload)
        {
            if (!string.IsNullOrEmpty(deviceToken) && deviceToken.Length != DEVICE_TOKEN_STRING_SIZE)
            {
                throw new NotificationFailureException(5, this);                 //Invalid DeviceToken Length
            }
            DeviceToken = deviceToken;
            Payload     = payload;

            Identifier = GetNextIdentifier();
        }
 public static AppleNotification WithPayload(this AppleNotification n, AppleNotificationPayload payload)
 {
     n.Payload = payload;
     return(n);
 }
        public static AppleNotification WithPayload(this AppleNotification n, AppleNotificationPayload payload)
        {
            n.Payload = payload;

            return n;
        }
        public static AppleNotification WithPasskitUpdate(this AppleNotification n)
        {
            var payLoad = new AppleNotificationPayload();
            payLoad.AddCustom("aps", string.Empty);

            n.Payload = payLoad;

            return n;
        }
        /// <summary>
        /// The main processor method used to process a single push notification, checks if the processing will be an immediate single push or regular thread looping model. 
        /// Looks up for a single (or more if you wish) entity in the databae which has not been processed. 
        /// Puts the fetched unprocessed push notification entity to processing over the Push Sharp API. 
        /// Finally saves the state of processing.
        /// </summary>
        /// <param name="databaseContext">The current database context to be used for processing to the database.</param>
        /// <param name="pushNotification">A single push notification entity to be processed and saved.</param>
        /// <param name="isDirectSinglePush">Decides wethere the processing will take place immediately for the sent notification or will the method lookup from the database for a first unprocessed push notification.</param>
        /// <returns>True if all OK, false if not.</returns>
        public bool ProcessNotification(PushSharpDatabaseContext dbContext, PushNotification pushNotification = null, bool isDirectSinglePush = false)
        {
            _databaseContext = dbContext;
            _isDirectSinglePush = isDirectSinglePush;

            if (_isDirectSinglePush)
                InitBroker();

            On(DisplayMessage, "Checking for unprocessed notifications...");
            PushNotification notificationEntity = pushNotification;

            try
            {
                if (notificationEntity != null)
                {
                    // save a new immediate unprocessed push notification
                    _databaseContext.PushNotification.Add(pushNotification);
                    _databaseContext.SaveChanges();

                    // reload the entity
                    notificationEntity = _databaseContext.PushNotification
                        .Where(x => x.ID == pushNotification.ID)
                        .Include(x => x.MobileDevice)
                        .Include(x => x.MobileDevice.Client)
                        .FirstOrDefault();
                }
                else // take one latest unprocessed notification, this can be changed to take any set size instead of one
                    notificationEntity = _databaseContext.PushNotification.FirstOrDefault(s =>
                        s.Status == (int)PushNotificationStatus.Unprocessed &&
                        s.CreatedAt <= DateTime.Now);
            }
            catch (Exception ex)
            {
                On(DisplayErrorMessage, "EX. ERROR: Check for unprocessed notifications: " + ex.Message);
                SimpleErrorLogger.LogError(ex);
            }

            // Process i.e. push the push notification via PushSharp... 
            if (notificationEntity != null)
            {
                bool messagePushed = true;

                On(DisplayStatusMessage, "Processing notification...");
                On(DisplayMessage, "ID " + notificationEntity.ID + " for " + notificationEntity.MobileDevice.Client.Username + " -> " + notificationEntity.Message);

                //---------------------------
                // ANDROID GCM NOTIFICATIONS
                //---------------------------
                if (notificationEntity.MobileDevice.SmartphonePlatform == "android")
                {
                    var gcmNotif = new GcmNotification() { Tag = notificationEntity.ID };
                    string msg = JsonConvert.SerializeObject(new { message = notificationEntity.Message });

                    gcmNotif.ForDeviceRegistrationId(notificationEntity.MobileDevice.PushNotificationsRegistrationID)
                            .WithJson(msg);

                    _broker.QueueNotification(gcmNotif);
                    UpdateNotificationQueued(notificationEntity);
                }
                ////-------------------------
                //// APPLE iOS NOTIFICATIONS
                ////-------------------------
                else if (notificationEntity.MobileDevice.SmartphonePlatform == "ios")
                {
                    var appleNotif = new AppleNotification() { Tag = notificationEntity.ID };
                    var msg = new AppleNotificationPayload(notificationEntity.Message);

                    appleNotif.ForDeviceToken(notificationEntity.MobileDevice.PushNotificationsRegistrationID)
                                .WithPayload(msg)
                                .WithSound("default");

                    _broker.QueueNotification(appleNotif);
                    UpdateNotificationQueued(notificationEntity);
                }
                //----------------------
                // WINDOWS NOTIFICATIONS
                //----------------------
                else if (notificationEntity.MobileDevice.SmartphonePlatform.Equals("wp") || notificationEntity.MobileDevice.SmartphonePlatform.Equals("wsa"))
                {
                    var wNotif = new WindowsToastNotification() { Tag = notificationEntity.ID };

                    wNotif.ForChannelUri(notificationEntity.MobileDevice.PushNotificationsRegistrationID)
                        .AsToastText02("PushSharp Notification", notificationEntity.Message);

                    _broker.QueueNotification(wNotif);
                    UpdateNotificationQueued(notificationEntity);
                }
                else
                {
                    On(DisplayErrorMessage, "ERROR: Unsupported device OS: " + notificationEntity.MobileDevice.SmartphonePlatform);

                    notificationEntity.Status = (int)PushNotificationStatus.Error;
                    notificationEntity.ModifiedAt = DateTime.Now;
                    notificationEntity.Description = "(Processor) Unsupported device OS: " + notificationEntity.MobileDevice.SmartphonePlatform;
                    SimpleErrorLogger.LogError(new Exception("EX. ERROR: " + notificationEntity.Description));
                    messagePushed = false;
                }

                try
                {
                    // Save changes to DB to keep the correct state of messages
                    _databaseContext.SaveChanges();

                    // bubble out the single push error, else return true to continue iteration
                    if (_isDirectSinglePush)
                        return messagePushed;

                    return true;
                }
                catch (Exception ex)
                {
                    On(DisplayErrorMessage, "EX. ERROR: Updating notification, DB save failed: " + ex.Message);
                    SimpleErrorLogger.LogError(ex);

                    // bubble out the single push error, else return true to continue iteration
                    if (_isDirectSinglePush)
                        return false;

                    return true;
                }
                finally
                {
                    if (_isDirectSinglePush)
                        KillBroker(_databaseContext);
                }
            }
            else
            {
                if (_isDirectSinglePush)
                    KillBroker(_databaseContext);

                // no messages were queued, take a nap...
                return false;
            }
        }