简体   繁体   English

在颤振本地通知中调用 HTTP 请求

[英]Call a HTTP request in flutter local notification

I would like to ask if there's a http get in flutter local notifications?我想问一下flutter本地通知中是否有http获取? My aim here is when the flutter notification will show, a http get request will be triggered我的目标是当显示颤振通知时,将触发 http get 请求

This my http request for my api:这是我对 api 的 http 请求:

final String url = 'http://192.168.43.45:8000/api';//url in my request
  List data;
  Future<String> getData() async {
    var response =await http.get(
      Uri.encodeFull(url),
      headers:{"Accept":"application/json"}   
    );//get data and decode it to json

  } 

This code will initialize the notifications:此代码将初始化通知:

      initializeNotifications() async {
        var initializationSettingsAndroid =
            AndroidInitializationSettings('@mipmap/launcher_icon');//icon will display when notification appears
        var initializationSettingsIOS = IOSInitializationSettings();
        var initializationSettings = InitializationSettings(
            initializationSettingsAndroid, initializationSettingsIOS);
        await flutterLocalNotificationsPlugin.initialize(initializationSettings,
            onSelectNotification: onSelectNotification);

      }

      Future onSelectNotification(String payload) async {
        if (payload != null) {
          debugPrint('notification payload: ' + payload);
        }

        await Navigator.push(
          context,
          new MaterialPageRoute(builder: (context) => HomePage()),
        );

      }

Code for the notification when click it will redirect to homePage:单击时通知的代码将重定向到主页:

      Future<void> scheduleNotification(Medicine medicine) async {
        var hour = int.parse(medicine.startTime[0] + medicine.startTime[1]);
        var ogValue = hour;
        var minute = int.parse(medicine.startTime[2] + medicine.startTime[3]);

        var androidPlatformChannelSpecifics = AndroidNotificationDetails(
          'repeatDailyAtTime channel id',
          'repeatDailyAtTime channel name',
          'repeatDailyAtTime description',
          importance: Importance.Max,
          ledColor: Color(0xFF3EB16F),
          ledOffMs: 1000,
          ledOnMs: 1000,
          enableLights: true,

        );


        //notification details 
        var iOSPlatformChannelSpecifics = IOSNotificationDetails();
        var platformChannelSpecifics = NotificationDetails(
            androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
          //this code will show the whats the notification must appear
          await flutterLocalNotificationsPlugin.showDailyAtTime(
              int.parse(medicine.notificationIDs[i]),
              'Mediminder: ${medicine.medicineName}',
              medicine.medicineType.toString() != 

MedicineType.None.toString()
                      ? 'It is time to take your Medicine, according to schedule'
                      : 'It is time to take your medicine, according to schedule',
                  Time(hour, minute,),
                  platformChannelSpecifics);
              hour = ogValue;

            }
            //await flutterLocalNotificationsPlugin.cancelAll();//cancel the flutter notifications
          }
        }

it seems that you are using flutter_local_notifications , looking at their documentation i don't think it's possible to you to handle the onReceive notification, this is done internally by the package.似乎您正在使用flutter_local_notifications ,查看他们的文档,我认为您无法处理 onReceive 通知,这是由包在内部完成的。

But maybe you could implement your own receiver class extending BroadCastReceiver and listen for the same action that the packages send, from there you'll be able to send HTTP requests.但也许您可以实现自己的接收器类扩展 BroadCastReceiver 并侦听包发送的相同操作,从那里您将能够发送 HTTP 请求。

Take a look at this question , maybe it helps.看看这个问题,也许它有帮助。



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM