gpt4 book ai didi

cordova - 当应用程序打开或应用程序在后台时,Phonegap Firebase 推送通知不会触发事件监听器

转载 作者:行者123 更新时间:2023-12-04 12:38:54 26 4
gpt4 key购买 nike

我正在使用 Phonegap 和 Firebase (fcm) 来接收推送通知。
我正在使用 Android 8 进行测试。

如果应用程序在前台运行(作为应用程序中的弹出消息),我希望我的设备收到推送通知。
如果应用程序关闭或在后台,我希望消息显示为通知,当我单击通知时,它会在前台打开应用程序并调用通知事件函数。

这是到目前为止发生的事情:

  • 当应用程序打开时,推送通知会通过并显示在应用程序中 - 正确
  • 当应用程序最小化时,顶部栏中会出现推送通知 - 正确...但是
  • 当我单击通知消息时,它会打开我的应用程序/将其置于前台,但未调用“通知”事件监听器 - 错误

  • 有很多关于这个问题的帮助请求,我经历了很多,但没有一个为我的应用程序提供有效的解决方案(大多数是在 2016 年到 2018 年之间,而不是 2019 年)。

    这主要需要适用于 Android。因此,我尝试删除通知负载并仅使用数据负载。然而这并不能解决问题。

    这是我当前的有效载荷:
    $notification = array
    (
    'body' => 'test',
    'title' => 'test',
    "icon" => 'notification_icon',
    "content_available" => true,
    );

    $data = array
    (
    "title" => "test",
    "body" => "test",
    "icon" => "notification_icon",
    "content_available" => true,
    );

    $fields = array
    (
    'registration_ids' => $registrationIds,
    'notification' => $notification,
    'data' => $data,
    "priority" => "high",
    "content_available" => true
    );

    据我了解,有 3 种方法可以发送通知:

    1) 作为“通知”。如果应用程序当前正在运行,这在前台运行,如果应用程序关闭或最小化,则在顶部栏中的后台运行(并且应用程序的图标上方有一个小徽章,表示一条消息),但在后台时,当消息被点击应用程序加载但通知功能未被调用时。

    2)作为“数据”。这似乎只在前台工作,没有顶部栏消息,应用程序图标上方没有小徽章。如果应用程序在后台,则根本没有推送通知。

    3) 作为“通知”和“数据”。在后台时,应用程序图标上方有一个徽章。单击顶部栏消息时,应用程序已加载,但未调用通知功能。

    此外,即使应用程序在后台,如果它收到推送通知并将应用程序置于前台,则不会显示通知消息。仅当收到通知时应用程序已在前台时才有效。

    这是我的事件监听器:
    var app = {
    initialize: function() {
    this.bindEvents();
    },
    bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
    console.log('Received Device Ready Event');
    app.setupPush();
    },
    setupPush: function() {
    var push = PushNotification.init({
    android: {
    senderID: 'XXXXXXXXXX',
    iconColor: "#ccc",
    alert: true,
    badge: true,
    icon: 'notification_icon',
    sound: true,
    vibrate: true,
    },
    browser: {},
    ios: {
    sound: true,
    vibration: true,
    badge: true
    },
    windows: {}
    });
    console.log('after init');

    push.on('registration', function(data) {
    //[edit - script not displayed for this example, but it registers a token to enable push notifications to the device. tested and working]
    });

    push.on('error', function(e) {
    console.log('push error = ' + e.message);
    });

    push.on('notification', function(data) {
    window.alert(data.additionalData); //Test line

    if(data.additionalData.foreground){
    window.alert(data.title+'\n'+data.message); //Test line
    }
    if(data.additionalData.coldstart){
    window.alert("coldstart"); //Test line
    }
    showPopup(data,"info");

    });


    }
    };

    //Using Sweet Alert to display messages

    function showPopup(data,type){
    Swal.fire({
    type: type,
    title: data.title,
    text: data.message,
    showConfirmButton: true,
    imageUrl: data.image,
    imageWidth: 400,
    imageHeight: 200,
    imageAlt: data.title,
    animation: true,
    });
    }

    有没有人对适用于 Android 7 和 8 的上述问题有答案?

    最佳答案

    您的通知发送逻辑是可靠的,但您的担忧也是有效的。如果您想阅读有关该主题的更多信息,请谷歌 notification vs data notifications ,有一些很好的文章解释它。

    推送通知在开放接收器上有一个特殊的 native ,您的 WebView 必须为这些事件注册。但是,如果您查看 source ,不处理通知打开事件。除了 fork 存储库之外,您无能为力。

    此外,.on('notification')当应用程序被终止时,您的 WebView 不会收到事件,因为……好吧,您的应用程序已被终止。您的应用在被终止时不会收到通知,Android 操作系统会收到通知并决定如何处理它。

    至于你为什么在 Android 8 上注意到这个问题,最近版本的 Android 更积极地在后台杀死应用程序,以节省电池:DozeAdaptive battery .较新的 android 应用程序很快就会被淘汰。

    如果可能,我建议您切换到 cordova-plugin-firebase ,一个更积极维护的插件。或者,学习一些 android 并编写自己的插件。

    关于cordova - 当应用程序打开或应用程序在后台时,Phonegap Firebase 推送通知不会触发事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55245060/

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