如何在Flutter中设置Android本机通知?

问题描述

是否可以使用firebase在Flutter中设置Android本机通知?我已经尝试过,并且当应用程序在前台时收到通知。但在应用未运行时却没有显示android native通知,但那时候却显示通知

这是firebaseMessing服务类

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        
        if (remoteMessage.getNotification() != null) {
            handleNow(remoteMessage);
            Log.e(TAG,"Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

    }

    @Override
    public void onNewToken(String token) {
        Log.e(TAG,"Refreshed token: " + token);

        sendRegistrationToServer(token);
    }


    private void handleNow(RemoteMessage remoteMessage) {


        String messageTitle = remoteMessage.getNotification().getTitle();
        String messageBody = remoteMessage.getNotification().getBody();



        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,"home")
                .setSmallIcon(R.drawable.googleg_disabled_color_18)
                .setContentTitle("Android Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(messageBody))
                .setLights(Color.WHITE,3000,3000)
                .setPriority(NotificationCompat.PRIORITY_MAX);

        notificationmanagerCompat notificationmanagerCompat = notificationmanagerCompat.from(this);
        notificationmanagerCompat.notify(1,mBuilder.build());



        int mNotificationID = (int)System.currentTimeMillis();
        notificationmanager manager = (notificationmanager)getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("home","Default channel",notificationmanager.IMPORTANCE_HIGH);

            manager.createNotificationChannel(channel);
        }
        manager.notify(mNotificationID,mBuilder.build());

        Log.e(TAG,"Short lived task is done.");
    }




    private void sendRegistrationToServer(String token) {
        // Todo: Implement this method to send token to your app server.
    }


}

这是我的AndroidManifest文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.Flutter_app_notification_test">
    <!-- io.Flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startinitialization(this); in its onCreate method.
         In most cases you can leave this as-is,but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.Flutter.app.FlutterApplication"
        android:label="Flutter_app_notification_test"
        android:icon="@mipmap/ic_launcher">

        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>



        <Meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/launch_background" />
        <Meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorPrimary" />

        <activity
            android:name=".MainActivity"
            android:launchMode="singletop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that,this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <Meta-data
              android:name="io.Flutter.embedding.android.normalTheme"
              android:resource="@style/normalTheme"
              />
            <!-- displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame,then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <Meta-data
              android:name="io.Flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the Meta-data below.
             This is used by the Flutter tool to generate GeneratedpluginRegistrant.java -->
        <Meta-data
            android:name="FlutterEmbedding"
            android:value="2" />
    </application>
</manifest>

我需要在应用未运行时显示android native通知,这是否有可能发生?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)