【发布时间】:2016-06-15 06:58:57
【问题描述】:
我正在尝试在 xamarin 项目中实现推送通知,我完成了设备令牌,我尝试使用 APNS & GCM Online Tester 进行通知测试,它成功发送通知,但没有进入设备,请告诉我哪里做错了,下面是我的代码
MyGcmListenerService
namespace GCMSample
{
[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
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.Icon)
.SetContentTitle("GCM Message")
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gcmsample" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
<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="com.gcmsample.permission.C2D_MESSAGE" />
<permission android:name="com.gcmsample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<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="com.gcmsample" />
</intent-filter>
</receiver>
<service
android:name="com.gcmsample.MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<application android:label="GCMSample"></application>
</manifest>
【问题讨论】:
-
推送通知时能否分享来自设备的 ADB 日志
-
我正在从apns-gcm.bryantan.info 推送通知,所以没有 ADB 日志
-
嗯,你仍然可以从设备developer.xamarin.com/guides/android/…获取ADB日志
-
@Prashant 这里是亚行日志drive.google.com/file/d/0BweRv1OeL66fVUtTeldiSU1mazA/…
标签: android xamarin push-notification xamarin.android