5

I'm trying to process GCM push notifications in Xamarin when the app is completely closed. Following the Xamarin Push Notifications tutorial I'm able to receive remote/push notifications from GCM, but as soon as I close the app I don't receive. Here's what I've tried so far:

1.Broadcast receiver:

    public class MyGCMBroadcastReceiver : BroadcastReceiver {

    public override void OnReceive (Context context, Intent intent)
    {
        Intent gcmListenerServiceIntent = new Intent(context,typeof(MyGcmListenerService));
        Console.WriteLine ("Starting Broadcast Receiver...");
        context.StartService (gcmListenerServiceIntent);
    }
}
  1. Manifest:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="za.co.snappyhome.snappy.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <application android:label="My App" android:theme="@style/AppTheme" android:icon="@drawable/icon">
    <receiver android:name="com.google.android.gms.gcm.GcmReceiver"
              android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="za.co.myapp.app" />
        </intent-filter>
    </receiver>
    <receiver android:name="MyApp.Droid.Notifications.MyGCMBroadcastReceiver">
        <intent-filter>
           <actionandroid:name="android.intent.action.BOOT_COMPLETED"/>                  
        </intent-filter>
    </receiver>
    </application>
    
  2. GCMListenerService:

    [Service (Exported = false), IntentFilter (new [] { "com.google.android.c2dm.intent.RECEIVE" })]
    public class MyGcmListenerService : GcmListenerService
    {
    
    public override void OnMessageReceived (string from, Bundle data)
    {
        if (data.ContainsKey ("data")) {
    
            if (Xamarin.Forms.Application.Current != null) {
                String json = data.GetString("data");
    
                MessagingCenter.Send<Xamarin.Forms.Application, string> (Xamarin.Forms.Application.Current,
                    "NOTIFICATION_MESSAGE_RECEIVED", json);                 
            }
        }
    }
    

    }

I'm still getting to grips with Xamarin and GCM notifications, so my understanding might be wrong. My understanding is that I can start up broadcast receiver when the app boots. That will in-turn start a service that can listen to push notifications(in my case MyGcmListenerService). My first issue is that the broadcast receiver doesn't start on application boot (following this answer: Trying to start a service on boot on Android). Secondly, is it possible to invoke MyGcmListenerService in order to start listening to notifications like what I'm trying to do. I've tried using GCMIntentService, but that seems to have been deprecated sometime ago: Push Notifications when app is closed

Thank you in advance!!

Community
  • 1
  • 1
nyatzanger
  • 121
  • 2
  • 5

1 Answers1

1

I used the below configuration and piece of code with Xamarin Native - Android and could see the notifications when the app is closed.

  1. Android manifest.xml

    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
            <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                    <category android:name="${applicationId}" />
                </intent-filter>
            </receiver>
    
    <service android:name="HEET.Droid.ViewController.Login.FireBaseIIDService" android:permission="false" android:stopWithTask="false">
                <intent-filter>
                    <action android:name="com.google.firebase.messaging_event" />
                </intent-filter>
            </service>
    
    1. FireBaseIIDService.cs

As per latest version , In the FireBaseIIDService intent service class, onMessageReceived(RemoteMessage msg) will be invoked on receiving every message.

        [Service]
        [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
        public class FireBaseIIDService : FirebaseMessagingService{

   public override void OnMessageReceived (RemoteMessage msg) {
Notification.Builder builder = new Notification.Builder(this, "my_notification_channel");
            builder.SetSmallIcon(Resource.Drawable.circle);
            var intent = new Intent(this, typeof(DashboardActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
            builder.SetContentIntent(pendingIntent);
            builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.circle));
            builder.SetContentTitle(Header);
            builder.SetContentText(body);
            builder.SetDefaults(NotificationDefaults.Sound);
            builder.SetAutoCancel(true);
            NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(1, builder.Build());
    }
    }
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
SPS
  • 31
  • 3
  • 1
    The sample I am following is C#/ Xamarin otherwise identical to this as far as I can see, but it definitely does NOT deliver the notiification if the app has been stopped, only foreground and background. Any ideas? – Journeyman1234 Sep 06 '19 at 13:54
  • I am able to receive notifications when the app is packaged in release mode while in debug mode even i am not able to receive if the app is stopped. – SPS Sep 09 '19 at 13:08
  • Nice tip but unfortunately having just tried that the results don't change for me - still cannot receive notifications in the system tray once the app has been force stopped – Journeyman1234 Sep 09 '19 at 15:34
  • Please post your code details.I will check if i would help you with my logic i built – SPS Sep 10 '19 at 09:32
  • Thanks - see existing thread here https://stackoverflow.com/questions/57824810/xamarin-forms-android-not-receiving-push-notifications-when-closed/57851121?noredirect=1#comment102159402_57851121 – Journeyman1234 Sep 10 '19 at 09:40