remotenotificationReceivedHandler未设置,显示正常的信号通知颤动

发布于 2025-02-03 15:30:48 字数 106 浏览 2 评论 0原文

处理发作的背景通知。当应用在后台时,我想在控制台中打印我的通知。对于前景通知,我使用了setNotificationwillShowinForegroundHandler。现在用于处理背景通知悬挂。

handle background notification in onesingnal flutter. i want to print my notification in console when app is in background. for foreground notification i used setNotificationWillShowInForegroundHandler. now for handling background notification flutter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2025-02-10 15:30:48

当该应用终止时,您无法打印任何内容,但是如果该应用程序在后台运行,则可以打印通知或通知其他数据。

在Android>中添加notificationserviceextension.jave文件app> src> Main> Kotlin>在MainAttivity旁边

添加以下代码:

package your bundle_id;

import android.content.Context;
import android.util.Log;
import org.json.JSONObject;

import com.onesignal.OSNotification;
import com.onesignal.OSMutableNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;



@SuppressWarnings("unused")
public class NotificationServiceExtension implements 
OSRemoteNotificationReceivedHandler {

@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
    OSNotification notification = notificationReceivedEvent.getNotification();
    Log.i("OneSignalExample", "Notification Data: " + notification);

    // Example of modifying the notification's accent color
    OSMutableNotification mutableNotification = notification.mutableCopy();
    mutableNotification.setExtender(builder -> {
        // Sets the accent color to Green on Android 5+ devices.

        //Force remove push from Notification Center after 30 seconds
        builder.setTimeoutAfter(30000);
        return builder;
    });
    JSONObject data = notification.getAdditionalData();
    //log.i will print data in body of notification
    Log.i("OneSignalExample", "Received Notification Data: " + data);
    
    // If complete isn't call within a time period of 25 seconds, 
    OneSignal internal logic will show the original notification
    // To omit displaying a notification, pass `null` to complete()
   


    notificationReceivedEvent.complete(mutableNotification);
   }
}

不要忘记在清单Android中的应用程序标签下方添加这些标签

<meta-data android:name="com.onesignal.NotificationServiceExtension"
               android:value="your_bundle_id.NotificationServiceExtension" />

when the app is terminated you can’t print anything but if the app is running in the background you can print notifications or notification additional data like this.

add NotificationServiceExtension.jave file in android > app > src > main > kotlin > next to mainActivity

add the following code:

package your bundle_id;

import android.content.Context;
import android.util.Log;
import org.json.JSONObject;

import com.onesignal.OSNotification;
import com.onesignal.OSMutableNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;



@SuppressWarnings("unused")
public class NotificationServiceExtension implements 
OSRemoteNotificationReceivedHandler {

@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
    OSNotification notification = notificationReceivedEvent.getNotification();
    Log.i("OneSignalExample", "Notification Data: " + notification);

    // Example of modifying the notification's accent color
    OSMutableNotification mutableNotification = notification.mutableCopy();
    mutableNotification.setExtender(builder -> {
        // Sets the accent color to Green on Android 5+ devices.

        //Force remove push from Notification Center after 30 seconds
        builder.setTimeoutAfter(30000);
        return builder;
    });
    JSONObject data = notification.getAdditionalData();
    //log.i will print data in body of notification
    Log.i("OneSignalExample", "Received Notification Data: " + data);
    
    // If complete isn't call within a time period of 25 seconds, 
    OneSignal internal logic will show the original notification
    // To omit displaying a notification, pass `null` to complete()
   


    notificationReceivedEvent.complete(mutableNotification);
   }
}

don't forget to add these tags just below application tag in manifest android

<meta-data android:name="com.onesignal.NotificationServiceExtension"
               android:value="your_bundle_id.NotificationServiceExtension" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文