0

I am developing an android app using Xamarin and for push notifications I am using PushSharp. I am having some trouble with receiving push notifications while the app is not running (after a reboot for example). Here is the Service code:

[BroadcastReceiver(Permission=GCMConstants.PERMISSION_GCM_INTENTS)]
    [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "com.xxx" })]
    [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "com.xxx" })]
    [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "com.xxx" })]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    public class PushHandlerBroadcastReceiver : PushHandlerBroadcastReceiverBase<PushHandlerService>
    {
        //IMPORTANT: Change this to your own Sender ID!
        //The SENDER_ID is your Google API Console App Project ID.
        //  Be sure to get the right Project ID from your Google APIs Console.  It's not the named project ID that appears in the Overview,
        //  but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
        //  where 785671162406 is the project id, which is the SENDER_ID to use!
        public static string[] SENDER_IDS = new string[] {"1234"};

        public const string TAG = "PushSharp-GCM";
    }

And here is the appManifest that is created:

 <receiver android:permission="com.google.android.c2dm.permission.SEND" android:name="xxx.PushHandlerBroadcastReceiver">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.xxx" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.xxx" />
      </intent-filter>
      <intent-filter>
        <action android:name="com.google.android.gcm.intent.RETRY" />
        <category android:name="com.xxx" />
      </intent-filter>
    </receiver>
    <service android:name="xxx.PushHandlerService" />

My service code is very basic:

[Service] //Must use the service tag
    public class PushHandlerService : PushHandlerServiceBase
    {
        public PushHandlerService () : base (PushHandlerBroadcastReceiver.SENDER_IDS)
        {
        }

        protected override void OnRegistered (Context context, string registrationId)
        {
            ...
        }

        protected override void OnUnRegistered (Context context, string registrationId)
        {
            ...

        }

        protected override void OnMessage (Context context, Intent intent)
        {
            ...
        }

        protected override bool OnRecoverableError (Context context, string errorId)
        {
            ...
        }

        protected override void OnError (Context context, string errorId)
        {
            ...
        }

        void createNotification (string title, string desc, Intent intent)
        {
            ...
        }
    }

Am I missing something? why is the service not started once the phone is rebooted. Should I be doing something in the broadcast receiver? Should I register to the push notifications in the service constructor (to handle the case where the app is not started yet)?

Amit Raz
  • 5,370
  • 8
  • 36
  • 63

1 Answers1

0

If your service does not start on reboot you can add a BroadcastReceiver to your project which starts it:

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class MyBootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        MyNotificationService.RunIntentInService(context, intent);
        SetResult(Result.Ok, null, null);
    }
}

If you are using PushSharp you can probably get away with adding that filter to the PushHandlerBroadcastReceiverBase implementation.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • I have tried that but it seems it ignores this type of broadcast receiver... I am struggling to figure out why... – Amit Raz May 03 '14 at 14:03
  • I've been working with notifications all day yesterday were I tried rebooting my device and could not reproduce the problem here when using the intent filter. So you must be doing something wrong or maybe you have a task manager killing stuff. – Cheesebaron May 03 '14 at 14:05
  • I will give it another shot, and see what happens – Amit Raz May 03 '14 at 14:26
  • I have tried it and it does not work :( I am very frustrated. I am simply not getting the boot_completed broadcast. – Amit Raz May 08 '14 at 09:20