android 推送通知不像Flutter中的Heads-up

nzkunb0c  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(173)

我想这样.

我使用FCM推送通知.消息到达,但不像抬头.它只出现在状态栏.我做错了什么,还是我必须做别的事情?

6za6bjd0

6za6bjd01#

你想在通知栏中显示你的应用通知吗?如果是,你应该试试这个软件包flutter_local_notifications: ^16.3.0
as you can show on top of image

cfh9epnr

cfh9epnr2#

要在Flutter中创建通知,您需要awesome_notifications
在您的主.dart文件中:

  1. AwesomeNotifications().initialize(
  2. // set the icon to null if you want to use the default app icon
  3. null,
  4. [
  5. NotificationChannel(
  6. channelGroupKey: 'channel_group_key',
  7. channelKey: 'channel_key',
  8. channelName: 'channel_name',
  9. channelDescription: 'channel_description',
  10. importance: NotificationImportance.Max,
  11. )
  12. ],
  13. debug: true,
  14. );

字符串
仅限Android初始化:

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.example">
  3. <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
  4. <application>
  5. ...
  6. <activity
  7. android:name=".MainActivity"
  8. android:showOnLockScreen="true"
  9. android:showWhenLocked="true"
  10. android:turnScreenOn="true">
  11. ...
  12. </activity>
  13. ...
  14. </application>
  15. </manifest>


注意事项:在iOS上,你需要请求通知权限。在Android 11上,你需要请求fullScreenIntent。下面是代码(将其放入initState()或其他):

  1. AwesomeNotifications().isNotificationAllowed().then(
  2. (isAllowed) {
  3. //It would be more appropriate if you can show your own dialog
  4. //to the user before requesting the notifications permissons.
  5. if (!isAllowed) {
  6. AwesomeNotifications().requestPermissionToSendNotifications(
  7. permissions: [
  8. NotificationPermission.Alert,
  9. NotificationPermission.Sound,
  10. NotificationPermission.Badge,
  11. NotificationPermission.Vibration,
  12. NotificationPermission.Light,
  13. NotificationPermission.FullScreenIntent,
  14. ],
  15. );
  16. }
  17. }
  18. );


以及如何创建通知,把它每当你需要消防通知:

  1. AwesomeNotifications().createNotification(
  2. content: NotificationContent(
  3. id: 10,
  4. channelKey: 'channel key' //Same as above in initilize,
  5. title: title,
  6. body: body,
  7. wakeUpScreen: true,
  8. fullScreenIntent: true,
  9. criticalAlert: true,
  10. //Other parameters
  11. ),
  12. );

展开查看全部

相关问题