【问题标题】:Push Notifications Not Arriving推送通知未到达
【发布时间】:2014-05-01 06:51:17
【问题描述】:

我正在使用 Xamarin 开发一个 android 应用程序,而对于推送通知,我正在使用 PushSharp。 在应用程序未运行时(例如重新启动后),我在接收推送通知时遇到了一些问题。这是服务代码:

[BroadcastReceiver(Permission=GCMConstants.PERMISSION_GCM_INTENTS)]
    [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "com.xxx" })]
    [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "com.xxx" })]
    [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "com.xxx" })]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    public class PushHandlerBroadcastReceiver : PushHandlerBroadcastReceiverBase<PushHandlerService>
    {
        //IMPORTANT: Change this to your own Sender ID!
        //The SENDER_ID is your Google API Console App Project ID.
        //  Be sure to get the right Project ID from your Google APIs Console.  It's not the named project ID that appears in the Overview,
        //  but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
        //  where 785671162406 is the project id, which is the SENDER_ID to use!
        public static string[] SENDER_IDS = new string[] {"1234"};

        public const string TAG = "PushSharp-GCM";
    }

这里是创建的 appManifest:

 <receiver android:permission="com.google.android.c2dm.permission.SEND" android:name="xxx.PushHandlerBroadcastReceiver">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.xxx" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.xxx" />
      </intent-filter>
      <intent-filter>
        <action android:name="com.google.android.gcm.intent.RETRY" />
        <category android:name="com.xxx" />
      </intent-filter>
    </receiver>
    <service android:name="xxx.PushHandlerService" />

我的服务代码很基础:

[Service] //Must use the service tag
    public class PushHandlerService : PushHandlerServiceBase
    {
        public PushHandlerService () : base (PushHandlerBroadcastReceiver.SENDER_IDS)
        {
        }

        protected override void OnRegistered (Context context, string registrationId)
        {
            ...
        }

        protected override void OnUnRegistered (Context context, string registrationId)
        {
            ...

        }

        protected override void OnMessage (Context context, Intent intent)
        {
            ...
        }

        protected override bool OnRecoverableError (Context context, string errorId)
        {
            ...
        }

        protected override void OnError (Context context, string errorId)
        {
            ...
        }

        void createNotification (string title, string desc, Intent intent)
        {
            ...
        }
    }

我错过了什么吗?为什么手机重启后服务没有启动。我应该在广播接收器中做些什么吗?我是否应该在服务构造函数中注册推送通知(以处理应用尚未启动的情况)?

【问题讨论】:

    标签: xamarin.android xamarin pushsharp


    【解决方案1】:

    如果您的服务在重新启动时未启动,您可以将 BroadcastReceiver 添加到启动它的项目中:

    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class MyBootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            MyNotificationService.RunIntentInService(context, intent);
            SetResult(Result.Ok, null, null);
        }
    }
    

    如果您使用的是PushSharp,则可以将该过滤器添加到PushHandlerBroadcastReceiverBase 实现中。

    【讨论】:

    • 我试过了,但它似乎忽略了这种类型的广播接收器......我正在努力找出原因......
    • 我昨天一整天都在处理通知,如果我尝试重新启动我的设备并且在使用意图过滤器时无法在此处重现问题。所以你一定是做错了什么,或者你有一个任务管理器杀死了东西。
    • 我再试一次,看看会发生什么
    • 我已经尝试过了,但它不起作用:(我很沮丧。我根本没有收到 boot_completed 广播。
    猜你喜欢
    • 2021-09-20
    • 2014-01-19
    • 1970-01-01
    • 2011-08-23
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多