Redth / PushSharp

A server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), Android (C2DM and GCM - Google Cloud Message), Windows Phone, Windows 8, Amazon, Blackberry, and (soon) FirefoxOS devices!
Other
4.38k stars 1.53k forks source link

Window service(window server 2k8) cannot send message to all IOS and Android devices at same time #421

Closed tuyenbuiqn closed 9 years ago

tuyenbuiqn commented 9 years ago

Hi guys,

I write a window service to push notification to all os flatform(ios,adroid) selected from database. When I test it in local PC, it works well. All devices are received message. But when I install this service to Window server 2k8, it doesn't work as I expect. The problem is when I push, only android devices received, ios devices aren't received any message! I try to change many ways to push it, but it still doesn't work.

  1. I only push to ios devices, it works well, all IOS devices are received message
  2. I only push to android devices, it also works well , all android devices are received message
  3. I push all ios and android devices, only android devices received message. And when I read the log file, IOS devices are sent successful. @@ It works well at local PC. This is my code, please let me know what happened.

    var push = new PushBroker(); push.OnNotificationSent += NotificationSent; push.OnChannelException += ChannelException; push.OnServiceException += ServiceException; push.OnNotificationFailed += NotificationFailed; push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; push.OnChannelCreated += ChannelCreated; push.OnChannelDestroyed += ChannelDestroyed; try { /// Get data from database MinfoDataContext dx = new MinfoDataContext(); List lstPushToPush = (from p in dx.CS_PushNotifications_GetToPush() select p).ToList();

            /// End Get data from database
            if (lstPushToPush.Count > 0)
            {
                PushSharpAlertAndroid objPushSharpAndroid = new PushSharpAlertAndroid();
                JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();

                /// Apple
                string fileP12 = "C://Websites//pushsharp//PushNotification//Certificates.p12";
                var appleCert = File.ReadAllBytes(fileP12);
                push.RegisterAppleService(new ApplePushChannelSettings(true, appleCert, ""));
                /// Android
                XmlDocument doc = new XmlDocument();
                string strFile = "C://Websites//pushsharp//PushNotification//AdroidCertificates.xml";
                doc.Load(strFile);
                XmlNode node = doc.DocumentElement.SelectSingleNode("/key");
                string androidKey = node.InnerText;
                string strAlert = "";
                push.RegisterGcmService(new GcmPushChannelSettings(androidKey));

                try
                {
                    foreach (CS_PushNotification item in lstPushToPush)
                    {
                        if (item.DeviceOS != null)
                        {
                            /// If device is Android flatform
                            if (item.DeviceOS.ToLower().Equals("android"))
                            {
                                objPushSharpAndroid.MinfoPush = item.Message + "##" + item.CampaignId + "##" + item.PushNofificationId;
                                objPushSharpAndroid.Badge = item.Badge.Value;
                                objPushSharpAndroid.Sound = item.SoundName;
                                strAlert = objJSSerializer.Serialize(objPushSharpAndroid);

                                push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(item.DeviceToken)
                                    .WithJson(strAlert));
                            }
                            /// If device is IOS
                            else if (item.DeviceOS.ToLower().Equals("ios"))
                            {
                                push.QueueNotification(new AppleNotification()
                                            .ForDeviceToken(item.DeviceToken)//the recipient device id
                                            .WithAlert(item.Message)//the message
                                            .WithBadge(item.Badge.Value)
                                            .WithSound(item.SoundName)
                                            .WithCustomItem("id", item.CampaignId)
                                            .WithCustomItem("Ids", item.PushNofificationId)
                                             );
                            }
                        }
                    }
                    push.StopAllServices(waitForQueuesToFinish: true);

                    dx.CS_PushNotifications_UpdateAfterPush_ByOS("android");
                }
                catch (Exception ex)
                {
                }
            }
        }
        catch (Exception ex)
        {
        }

Hope to see good news from you. Thank for reading. Please help me, thank you very much!

Redth commented 9 years ago

Need more info, but I suspect you're mixing up the Sandbox/Production push certificates and iOS app being compiled with either AdHoc or App Store provisioning profile.

You must send APNS with Production cert (to production server) if the device token you get is from an app signed with AdHoc or AppStore provisioning profile. Otherwise, use Sandbox certificate and connect to sandbox servers.