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

5
我将尝试在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. 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>
    
  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);                 
            }
        }
    }
    

    }

我还在努力掌握Xamarin和GCM通知,所以我的理解可能有误。我理解的是,当应用启动时,可以启动广播接收器,然后这会进而启动一个服务来监听推送通知(在我的情况下是MyGcmListenerService)。我的第一个问题是,广播接收器在应用启动时并没有启动(按照以下答案:Trying to start a service on boot on Android)。其次,是否可以调用MyGcmListenerService来开始监听通知,就像我正在尝试的那样。我已经尝试使用GCMIntentService,但这似乎已经过时了: Push Notifications when app is closed 非常感谢你的帮助!
1个回答

1

我在使用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());
    }
    }

1
我正在遵循的示例是C# / Xamarin,否则在我看来与此完全相同,但它绝对不会在应用程序停止时提供通知,只有前台和后台。有什么想法吗? - Journeyman1234
即使应用程序停止,我仍能在发布模式下接收通知,而在调试模式下则无法接收。 - SPS
不错的提示,但不幸的是,我刚试过了,结果对我来说没有改变 - 一旦应用程序被强制停止,我仍然无法在系统托盘中接收通知。 - Journeyman1234
请发布您的代码细节。我会检查是否能用我构建的逻辑来帮助您。 - SPS
感谢您,这里有现有的线程 https://dev59.com/ibbna4cB1Zd3GeqPWSr7#57851121?noredirect=1#comment102159402_57851121 - Journeyman1234

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接