gpt4 book ai didi

android - StatusBarNotification 如何获取数据或重新发送 Intent ?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:58 27 4
gpt4 key购买 nike

我的应用程序通过 Firebase 接收推送通知。现在,当通知到达时,可能会发生 3 种不同的情况:

  1. 应用在前台
  2. 应用在后台
  3. 应用没有运行

情况1没问题。通知在应用程序中收到 ok。只要您点击抽屉中的通知,情况 2 和 3 就可以正常工作。在情况 2 和 3 中,当点击应用程序图标而不是抽屉图标时,应用程序根本不会收到任何通知。我已尝试从 StatusBar 获取 Activity 通知并且有效,但我无法从 Extras 检索数据或将通知重新发送到等待的推送通知服务。这是获取通知的实验代码。

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

var notifications = notificationManager.GetActiveNotifications()
.Where(notif => notif.PackageName == Application.Context.PackageName);

foreach (var notification in notifications)
{
Log.Info(TAG, "OnActivityResumed: Notification in active in Status Bar: {0}", notification.Notification.ToString());

var data = notification.Notification.Extras.GetString("data");

Log.Debug("Notifier", "Data received: {0}", data);

//if (data != null)
//{
// Settings.Notification = JsonConvert.DeserializeObject<LoginNotificationParameter>(data);
//}
}

// Canceling all notifications
notificationManager.CancelAll();

问题:

  1. 当抽屉中有通知时,应用不接收任何 Intent 的行为是否正确?
  2. 如果是这样,当用户点击应用图标而不是抽屉通知时,我该如何处理情况 2 和 3?

我在打印所有 Extras 键值时在 het Notification 中看到了这些数据:

06-28 16:34:13.174 20792 20792 I Notifier: OnActivityResumed: Notification is active in Status Bar: Notification(pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE semFlags=0x0 semPriority=0)
06-28 16:34:13.191 20792 20792 I Notifier: KeyIn: Extras Key: android.title Data: Login
06-28 16:34:13.191 20792 20792 I Notifier: KeyIn: Extras Key: android.subText Data:
06-28 16:34:13.191 20792 20792 I Notifier: KeyIn: Extras Key: android.template Data: android.app.Notification$BigTextStyle
06-28 16:34:13.192 20792 20792 I Notifier: KeyIn: Extras Key: android.showChronometer Data: false
06-28 16:34:13.192 20792 20792 I Notifier: KeyIn: Extras Key: android.text Data: Er is een inlogverzoek voor u ontvangen.
06-28 16:34:13.194 20792 20792 I Notifier: KeyIn: Extras Key: android.progress Data: 0
06-28 16:34:13.194 20792 20792 I Notifier: KeyIn: Extras Key: android.progressMax Data: 0
06-28 16:34:13.195 20792 20792 I Notifier: KeyIn: Extras Key: android.appInfo Data: ApplicationInfo{a27f281 nl.natuurnetwerk.notifier}
06-28 16:34:13.195 20792 20792 I Notifier: KeyIn: Extras Key: android.showWhen Data: true
06-28 16:34:13.195 20792 20792 I Notifier: KeyIn: Extras Key: android.largeIcon Data:
06-28 16:34:13.195 20792 20792 I Notifier: KeyIn: Extras Key: android.bigText Data: Er is een inlogverzoek voor u ontvangen.
06-28 16:34:13.195 20792 20792 I Notifier: KeyIn: Extras Key: android.infoText Data:
06-28 16:34:13.195 20792 20792 I Notifier: KeyIn: Extras Key: android.originatingUserId Data: 0
06-28 16:34:13.196 20792 20792 I Notifier: KeyIn: Extras Key: android.progressIndeterminate Data: false
06-28 16:34:13.196 20792 20792 I Notifier: KeyIn: Extras Key: android.remoteInputHistory Data:

最佳答案

好的,这适用于我能想到的所有应用程序状态:

using Android.App;
using Android.Content;
using Android.OS;
using MvvmCross.Droid.Platform;
using MvvmCross.Droid.Support.V4;

namespace Notifier.Android.Classes.Services
{
/// <summary>
/// This class receives all Firebase intents.
/// </summary>
[BroadcastReceiver(Enabled = true, Permission = "com.google.android.c2dm.permission.SEND")]
[IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "com.yourdomain.yourapp" })]
public class FirebaseBroadcastReceiver : MvxWakefulBroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(context);
setup.EnsureInitialized();

var service = new Intent(context, typeof(FirebaseBroadcastReceiver));

PowerManager.WakeLock sWakeLock;
var pm = PowerManager.FromContext(context);
sWakeLock = pm.NewWakeLock(WakeLockFlags.Full, "FirebaseBroadcastReceiver");
sWakeLock.Acquire();


if (intent.Extras != null)
{
// Your code to handle the intent
}

sWakeLock.Release();

StartWakefulService(context, service);
}
}
}

关于android - StatusBarNotification 如何获取数据或重新发送 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44803954/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com