我将尝试在Xamarin中处理GCM推送通知,即使应用程序完全关闭也能够接收到。遵循Xamarin推送通知教程,我能够从GCM接收远程/推送通知,但是一旦我关闭应用程序,就无法接收通知。到目前为止,我尝试了以下方法:
1.广播接收器:
1.广播接收器:
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);
}
}
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>
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); } } }
}