关闭时如何在通知点击中打开应用

时间:2019-09-30 13:22:35

标签: c# android forms xamarin

我一直在尝试在关闭时单击该应用程序,即使该应用程序处于关闭状态,我也会收到FCM通知,但是单击该按钮时却没有任何反应。如果应用正在运行或在后台运行,它将按预期响应。

我正在运行Xamarin Forms,在Android中,我不知道关闭该应用程序后会发生什么,因为我没有调试。

//SendNotifications
public void SendNotification(string body)
{

using (var notificationManager = NotificationManager.FromContext(BaseContext))
{
                var intent = new Intent(this, typeof(MainActivity));
                intent.AddFlags(ActivityFlags.ClearTop);
                intent.PutExtra("SomeSpecialKey", "some special value");
                var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
// sends notification to view model
                MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase");

                //PendingIntent pendingIntent = PendingIntent.GetActivity(this.ApplicationContext, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);
                Notification notification;
                if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
                {

                    notification = new Notification.Builder(BaseContext)
                                                                .SetContentTitle("Notification")
                                                                .SetContentText(body)
                                                                .SetAutoCancel(true)
                                                                .SetSmallIcon(Resource.Drawable.icon)
                                                                .SetDefaults(NotificationDefaults.All)
                                                                .SetContentIntent(pendingIntent)
                                                                .Build();

                }
                else
                {
                    var myUrgentChannel = BaseContext.PackageName;
                    const string channelName = "CanalComun";

                    NotificationChannel channel;
                    channel = notificationManager.GetNotificationChannel(myUrgentChannel);
                    if (channel == null)
                    {
                        channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
                        channel.EnableVibration(true);
                        channel.EnableLights(true);
                        channel.SetSound(
                            RingtoneManager.GetDefaultUri(RingtoneType.Notification),
                            new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
                        );
                        channel.LockscreenVisibility = NotificationVisibility.Public;
                        notificationManager.CreateNotificationChannel(channel);
                    }
                    channel?.Dispose();

                    notification = new Notification.Builder(BaseContext)
                                                                .SetChannelId(myUrgentChannel)
                                                                .SetContentTitle("Notification")
                                                                .SetContentText(body)
                                                                .SetAutoCancel(true)
                                                                .SetSmallIcon(Resource.Drawable.icon)
                                                                .SetContentIntent(pendingIntent)
                                                                .Build();
                }
                notificationManager.Notify(1331, notification);
                notification.Dispose();
            }

如果已关闭,则有望打开该应用程序,但是由于您的帮助,我无法正常运行

0 个答案:

没有答案