当应用程序在xamarin中关闭时接收推送通知

时间:2016-05-02 16:48:49

标签: android google-cloud-messaging xamarin.forms

我在应用程序完全关闭时尝试在Xamarin中处理GCM推送通知。按照Xamarin推送通知教程,我能够从GCM接收远程/推送通知,但是一旦我关闭应用程序,我就无法收到。这是我到目前为止所尝试的内容:

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);
    }
}
  1. 清单:

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

    }

  3. 我还在处理Xamarin和GCM通知,所以我的理解可能是错误的。我的理解是,我可以在应用程序启动时启动广播接收器。这将反过来启动一个可以监听推送通知的服务(在我的情况下是MyGcmListenerService)。我的第一个问题是广播接收器没有在应用程序启动时启动(遵循此答案:Trying to start a service on boot on Android)。其次,是否可以调用MyGcmListenerService以便开始收听我尝试做的通知。我尝试过使用GCMIntentService,但这似乎已经被弃用了: Push Notifications when app is closed

    提前谢谢!!

1 个答案:

答案 0 :(得分:0)

我在Xamarin Native-Android上使用了以下配置和代码段,并且在应用关闭时可以看到通知。

  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

根据最新版本,在FireBaseIIDService意向服务类中,          接收到每条消息时都会调用onMessageReceived(RemoteMessage msg)。

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