Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

receiving push notifications when app is closed in xamarin

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!!

like image 747
nyatzanger Avatar asked Nov 08 '22 15:11

nyatzanger


1 Answers

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());
    }
    }
like image 174
SPS Avatar answered Nov 14 '22 21:11

SPS