Xamarin c#Android通知恢复后台活动

时间:2016-10-15 22:38:34

标签: c# android xamarin push-notification

我正在创建一个应用,在回原点时执行某些操作,并在特定情况下发送本地通知。

现在,我想在点击通知时恢复应用。

我在StackOverflow中看到了三个线程,并尝试使用标志,但没有人工作。

这就是我的尝试:

 Intent resultIntent = new Intent(this, typeof(MainActivity));

        TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
        stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
        stackBuilder.AddNextIntent(resultIntent);

        PendingIntent resultPendingIntent = 
            stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.CancelCurrent);

        Notification.Builder builder = new Notification.Builder(this)
            .SetContentTitle("Nuove Richieste")
            .SetContentText("Hai nuove richieste da visionare")
            .SetContentIntent(resultPendingIntent)
            .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
            .SetSmallIcon(Resource.Drawable.add)
            .SetPriority(1);

        Notification notification = builder.Build();

        NotificationManager notificationManager =
            GetSystemService(Context.NotificationService) as NotificationManager;

        const int notificationId = 0;
        notificationManager.Notify(notificationId, notification);

点击通知后,会出现黑屏,出了什么问题?

谢谢

编辑:

问题是另一个问题,仍然是关于恢复活动,但问题不同:

我有一个TIMER每10秒执行一次数据库查询。如果未杀死此计时器,则无法显示新活动(或恢复旧活动)。

我可以在创建通知时轻松杀死计时器。

顺便说一下,问题是没有通知但不是问题的情况。我将打开另一个问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

您只是取消了您的活动,因此没有任何错误。

尝试使用:

  

PendingIntentFlags.UpdateCurrent

你的代码中的

  

PendingIntent resultPendingIntent =               stackBuilder.GetPendingIntent(0,   (INT)PendingIntentFlags.UpdateCurrent);

阅读this了解详情。

相关问题