簡體   English   中英

Xamarin C#Android Notification在后台恢復活動

[英]Xamarin c# Android Notification resume activity in background

我正在制作一個應用程序,該應用程序在后台運行時會執行一些操作,並在特定情況下發送本地通知。

現在,我想在單擊通知時恢復應用程序。

我在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);

單擊通知后,出現黑屏,這是怎么回事?

謝謝

編輯:

問題是另一個,仍然與恢復活動有關,但是問題有所不同:

我有一個計時器,每10秒執行一次數據庫查詢。 如果沒有終止此計時器,則無法顯示新活動(或恢復舊活動)。 創建通知時,我可以輕松殺死計時器,因此問題得到解決。

順便說一句,問題在於沒有通知但不是問題的情況。 我將打開另一個問題。

謝謝!

您只需取消活動,就不會顯示任何內容。

嘗試使用:

PendingIntentFlags.UpdateCurrent

在您的代碼中:

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

閱讀以獲得更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM