Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sent messages are not received #26

Closed
EnemyArea opened this issue Aug 10, 2012 · 16 comments
Closed

Sent messages are not received #26

EnemyArea opened this issue Aug 10, 2012 · 16 comments

Comments

@EnemyArea
Copy link

Hi,
if you try to send some messages in a short time, like for(...) QueueNotification()
not all messages are sent to the device

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

Going to need a lot more info on this one...

@EnemyArea
Copy link
Author

ok, i try :) here some code:

public class DeviceSettingsManager
{
private PushServiceAndroid pushServiceAndroid;

    public void SetAndroidSettings(string senderID, string authToken, string packageName)
    {
        pushServiceAndroid = new PushServiceAndroid(new GcmPushChannelSettings(senderID, authToken, packageName));
    }

    public void AddMessageToQueue(string registrationID, string message)
    {
        var noti = NotificationFactory.AndroidGcm();
        noti.RegistrationIds.Add(registrationID);
        noti.JsonData = message;
        noti.CollapseKey = Guid.NewGuid().ToString();
        push.QueueNotification(noti);
    }

    public void SendMessages()
    {
        //Stop and wait for the queues to drains
        push.StopAllServices();
    }

}

this is used to prepare the messages to be sent

now I add some Messages
var pushServiceAndroid= new DeviceSettingsManager();
pushServiceAndroid.SetAndroidSettings(...);
pushServiceAndroid.AddMessageToQueue("Device1", "Message");
pushServiceAndroid.AddMessageToQueue("Device2", "Message");
pushServiceAndroid.AddMessageToQueue("Device3", "Message");
pushServiceAndroid.AddMessageToQueue("Device4", "Message");
pushServiceAndroid.SendMessages();

Only the first device got the message and if you add "OnOnNotificationSent"-Event it only shows the first message.
But if you add an Thread.Sleep(2000) between the pushServiceAndroid.AddMessageToQueue-calls it work !?

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

You are mixing C2DM classes with GCM classes.

Make sure you are using the GcmPushService if you are using GcmPushChannelSettings

I will add some type detection to make sure you get an exception in the future if you are using the wrong classes.

@EnemyArea
Copy link
Author

Hi, ok I have change my class to GcmPushService but it does not change :(

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

Please see the latest code / NuGet release 1.0.10. I've renamed some of the Android classes to be C2dm explicitly, and there's now type safety in the ctor's to ensure you don't use the incorrect settings types.... This should make it harder to accidentally use the wrong classes, but will require a few changes in your code

@EnemyArea
Copy link
Author

Hi,
i've upgrade to 1.0.10 but still no changes :(

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

Can you show me the code you use now?

On Fri, Aug 10, 2012 at 10:00 AM, DarkPrisma notifications@github.comwrote:

Hi,
i've upgrade to 1.0.10 but still no changes :(


Reply to this email directly or view it on GitHubhttps://github.com//issues/26#issuecomment-7642734.

@EnemyArea
Copy link
Author

using System;
using PushSharp;
using PushSharp.Android;
using PushSharp.Common;

namespace PushServerTest
{
class Program
{
static GcmPushService push;

    static void Main(string[] args)
    {
        // Fill it with your senderID, authToken, packageName :)
        push = new GcmPushService(new GcmPushChannelSettings("xxxxx", "xxxxx", "xxxx"));
        push.Events.OnNotificationSent += OnNotificationSent;

        // Add some messages
        AddMessageToQueue("xxxx1", "{\"alert\":\"Alert Text1!\",\"badge\":\"1\"}");
        AddMessageToQueue("xxxx2", "{\"alert\":\"Alert Text1!\",\"badge\":\"2\"}");
        AddMessageToQueue("xxxx3", "{\"alert\":\"Alert Text1!\",\"badge\":\"3\"}");

        // Send them
        SendMessages();

        Console.ReadLine();
    }


    static void AddMessageToQueue(string registrationID, string message)
    {
        var noti = NotificationFactory.AndroidGcm();
        noti.RegistrationIds.Add(registrationID);
        noti.JsonData = message;
        noti.CollapseKey = Guid.NewGuid().ToString();
        push.QueueNotification(noti);

        Console.WriteLine("AddMessageToQueue: " + noti.CollapseKey);
    }


    static void OnNotificationSent(Notification notification)
    {
        var gcmNotification = (GcmNotification)notification;
        Console.WriteLine("OnNotificationSent: " + gcmNotification.CollapseKey);
    }


    static void SendMessages()
    {
        // Message
        Console.WriteLine("Android Service started");

        //Stop and wait for the queues to drains
        push.Stop(true);

        // Message
        Console.WriteLine("Android Service complete");
    }
}

}

it prints
Android Service started
OnNotificationSent
Android Service complete

but it should be
Android Service started
OnNotificationSent
OnNotificationSent
OnNotificationSent
Android Service complete

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

Please also register the following events and log them to Console.WriteLine and let me know if there's any more data:

push.Events.OnDeviceSubscriptionExpired
push.Events.OnDeviceSubscriptionIdChanged
push.Events.OnChannelException
push.Events.OnNotificationSendFailure

@EnemyArea
Copy link
Author

i've added them, but no change :( no new messages shown

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

Ok let me do some testing and see if I can reproduce your results

On Fri, Aug 10, 2012 at 10:28 AM, DarkPrisma notifications@github.comwrote:

i've added them, but no change :( no new messages shown


Reply to this email directly or view it on GitHubhttps://github.com//issues/26#issuecomment-7646061.

@Redth
Copy link
Owner

Redth commented Aug 10, 2012

It's not a perfect solution for now, but try putting a Thread.Sleep() before you call push.Stop(true). Maybe sleep for 2-3 seconds? In most cases I'd recommend you keep the instance of the service around instead of stopping it after you send messages.

I'll take a look and see why Stop(true)... seems not to be waiting for the Queue to actually finish in the meantime...

@EnemyArea
Copy link
Author

Hi, it seems that the problem is the Stop-Method of PushChannelBase.
If u disable these lines:

        if (!CancelTokenSource.IsCancellationRequested)
            CancelTokenSource.Cancel();

it work as well. But why ? I think its simple, you make an async call to the google server because of the QueuedNotificationCount return 0 if no new messages are in it. This invokes the CancelTokenSource.Cancel(); and all running requests to the google server are canceled. We could maybe count the messages, to get save that we have send them all.

EDIT: I have implemented some dirty counter and now it works :D
the only thing that does not work is if I send 300 or more messages. Then he sends only 299, same if you try to send 3000 he send only 2999 :(

EDIT2: Ok, it seems to be the same here, he delete the channels, which are working. If I disable the code in the TeardownChannel-Method, its all ok

@Redth
Copy link
Owner

Redth commented Aug 18, 2012

I will look at this as soon as I can, unfortunately I'm out of town for a week right now.

@Redth Redth closed this as completed in fa72de5 Aug 28, 2012
@Redth
Copy link
Owner

Redth commented Aug 28, 2012

So i did make some changes. I implemented a message counter for GCM and C2DM (since they both have the same side effect from sending messages ASYNC). I've also changed how the .Stop() method works a bit, in the PushChannelBase - Sender now doesn't exit until the notification Queue is zero, but also you will now get an exception if you try to queue a notification on a channel after you've already called stop on it.

Please try your code again, it should work now. Thanks for digging up this issue too, as I believe it would have caused problems in iOS and WP7 as well :)

@EnemyArea
Copy link
Author

Thank u! Now it works :) Great job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants