GardenMobile & interfaces

An older Flutter OneSignal notification example

Historical receive/open callbacks and player-ID access from the shared-instance API.

A notification being received and a notification being opened are different events. This earlier Flutter example logged them through separate callbacks on OneSignal.shared.

Read the historical notification demo
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _debugLabelString = "";

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    if (!mounted) return;

    OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);

    var settings = {
      OSiOSSettings.autoPrompt: false,
      OSiOSSettings.promptBeforeOpeningPushUrl: true
    };

    OneSignal.shared
        .setNotificationReceivedHandler((OSNotification notification) {
      this.setState(() {
        _debugLabelString =
            "Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
      });
    });

    OneSignal.shared
        .setNotificationOpenedHandler((OSNotificationOpenedResult result) {
      this.setState(() {
        _debugLabelString =
            "Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
      });
    });

    // NOTE: Replace with your own app ID from https://www.onesignal.com
    await OneSignal.shared
        .init("EXAMPLE_ONESIGNAL_APP_ID", iOSSettings: settings);

    OneSignal.shared
        .setInFocusDisplayType(OSNotificationDisplayType.notification);
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('OneSignal Flutter Demo'),
          backgroundColor: Color.fromARGB(255, 212, 86, 83),
        ),
        body: new Container(
          child: new Text(_debugLabelString),
          alignment: Alignment.center,
        ),
      ),
    );
  }
}

The application identifier has been replaced with an example value. The source configured iOS prompting and foreground presentation as part of its older SDK integration. Those choices do not establish a permission policy for a current application.

The earlier player-ID lookup

OneSignal.shared.getPermissionSubscriptionState().then((response) {
    String playerId = response.subscriptionStatus.userId.toString();

});

This records the old API’s identifier field. It does not prove that a device is reachable or that a message was delivered. Use the OneSignal Flutter documentation when choosing an actual SDK version; no push service was called for this revision.

← Back to the garden