未调用Xamarin GCM OnMessageReceived

时间:2016-10-04 07:58:36

标签: c# android xamarin google-cloud-messaging xamarin.android

我使用此link实施了Xamarin远程通知,但我无法从GCM获取消息。我错过了什么?

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());
    }
}

我的清单文件:

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

我的服务器收到了message_id的成功响应,但是没有调用OnMessageReceived方法,我可以获得令牌。

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。这是我的错误

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

应该是

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

应用程序标签是接收方的父级

答案 1 :(得分:0)

Application标签是AndroidMenifest.xml上的Receiver的父级!

对于FCM和GCM具有相同的情况。

&LT;应用程序android:allowBackup =“true”android:icon =“@ mipmap / icon”android:label =“@ string / app_name”&gt;

- 开始手动Firebase添加 -

&LT;接收器android:name =“com.google.firebase.iid.FirebaseInstanceIdInternalReceiver”...........................

- END手动Firebase添加 - &LT; / application&gt;