【问题标题】:xamarin Push Notification Issuexamarin 推送通知问题
【发布时间】:2017-02-27 07:07:30
【问题描述】:

您好,我正在使用Push Notification Plugin for Xamarin 在我的应用程序中实现推送通知,并且我正在接收推送通知(使用 GCM),但问题是我发送到设备的推送通知在我发送新通知时被替换,并且不是预期的行为,我想显示收到的所有通知,这就是我配置我的 android 应用程序的方式

public override void OnCreate()
    {
        base.OnCreate();
        AppContext = this.ApplicationContext;
        CrossPushNotification.NotificationContentTextKey = "message";
        CrossPushNotification.NotificationContentTitleKey = "contentTitle";
        CrossPushNotification.NotificationContentDataKey = "data";
        CrossPushNotification.Initialize<CrossPushNotificationListener>("MYANDROIDSENDERID");
        //This service will keep your app receiving push even when closed.             
        CrossPushNotification.Current.Register();
        StartPushService();
        RegisterActivityLifecycleCallbacks(this);

    }
    public static void StartPushService()
    {
        AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));

        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
        {
            Random rnd = new Random();
            // Publish the notification:
            PendingIntent pintent = PendingIntent.GetService(AppContext,0, new Intent(AppContext, typeof(PushNotificationService)),0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            alarm.Cancel(pintent);
        }
    }

我错过了什么?

【问题讨论】:

  • 您收到一个推送通知然后发送第二个,或者您同时发送两个到 GCM?还请告诉我,您是否打开了第一个通知?您希望从以前的通知中保留哪些内容?
  • 我收到一个推送通知,但我什么也没做,然后我在一段时间后发送第二个,而不是显示两个通知我只能看到最后一个发送到设备的通知..跨度>

标签: c# xamarin xamarin.forms


【解决方案1】:

这是来自 google 的设计模式(我听说这同样适用于 iOS),因此您可以收到来自本机代码的多个通知,但您的应用将被 Play 商店拒绝并且不遵循 Google 的模式,因此这样做在为 Xamarin 编写的推送通知插件中是不可能的。

有关更多信息,请参阅thisthis

【讨论】:

    【解决方案2】:

    实际上这是可能的,我必须使用特定平台来实现这一点。我没有在实现IPushNotificationListener 接口的PCL 中定义CrossPushNotificationListener,而是在droid 项目中创建了另一个类并实现了IPushNotificationListener 接口,然后使用它来初始化pushnotification,然后使用OnMessage 方法我添加了代码以使用Notification.Builder 生成通知,如下所示

    void IPushNotificationListener.OnMessage(JObject parameters, DeviceType deviceType)
        {
            try
            {
    
                Intent intent = new Intent(Android.App.Application.Context, typeof(MainActivity));
                Intent[] intar = { intent };
                PendingIntent pintent = PendingIntent.GetActivities(Android.App.Application.Context, 0, intar, 0);
                intent.SetAction("notification");
                //Json Response Recieved 
                Dictionary<string, string> results = JsonConvert.DeserializeObject<Dictionary<string, string>>(parameters.ToString());
                Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)
         .SetContentTitle(results["contentTitle"])
         .SetContentText(results["message"])
         .SetContentIntent(pintent)
         .SetSmallIcon(Resource.Drawable.icon);
                // Build the notification:
                Notification notification = builder.Build();
    
                //Clear notification on click
                notification.Flags = NotificationFlags.AutoCancel;
    
                // Get the notification manager:
                NotificationManager notificationManager =
                    Android.App.Application.Context.GetSystemService(PushNotificationService.NotificationService) as NotificationManager;
               //notificationId  need to be unique for each message same ID will update existing message
                int notificationId = Convert.ToInt32(results["Id"]);
                // Publish the notification:
                notificationManager.Notify(notificationId, notification);
            }
            catch (Exception)
            {
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多