0

I implemented Xamarin Remote notification using this link, but I can't get messages from GCM. What did I miss?

My ListenerService:

 [Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "MYPACKGAGE NAME IS HERE I WROTE" })]
public class MyGcmListenerService : GcmListenerService
{
    public override void OnMessageReceived(string from, Bundle data)
    {
        var message = data.GetString("message");
        Log.Debug("MyGcmListenerService", "From:    " + from);
        Log.Debug("MyGcmListenerService", "Message: " + message);
        SendNotification(message);
    }

    void SendNotification(string message)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.unknown)
            .SetContentTitle("GCM Message")
            .SetContentText(message)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

        var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

My manifest file:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="MYPACKGAGE NAME" android:installLocation="auto" android:versionCode="1" android:versionName="1">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="MYPACKGAGE NAME.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission android:name="MYPACKGAGE NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<application android:label="MYAPP" android:icon="@drawable/Icon"></application>
<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="MYPACKGAGE NAME" />
    </intent-filter>
</receiver></manifest>

My server received a success response with a message_id, but that OnMessageReceived method isn't called and I can get token.

AL.
  • 36,815
  • 10
  • 142
  • 281

2 Answers2

0

I find the solution. It's my mistake

<application android:label="MYAPP" android:icon="@drawable/Icon">/application>
<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="MYPACKGAGE NAME" />
</intent-filter>
</receiver>
</manifest>

it should be

<application android:label="MYAPP" 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="MYPACKGAGE NAME" />
</intent-filter>
</receiver>
</application>
</manifest>

application tag is parent of reciever

0

Application tag is parent of Receiver at AndroidMenifest.xml !

For FCM and GCM are having the same case.

< application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">

-- START Manual Firebase Additions --

< receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"...........................

-- END Manual Firebase Additions -- < / application>

Ethan Liew
  • 91
  • 1
  • 3