Why don’t push notifications from my app using one Signal arrive with the app closed or in the background?

Asked

Viewed 156 times

0

I’m using one Signal to send the push notification to my app, I saw several tutorials to create the notifications and I did so : configuration of

app/build Gradle:


dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    implementation 'com.google.firebase:firebase-messaging:17.5.0'

}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: 'com.google.gms.google-services'

android/buildgradle configuration:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }

    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.3'
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.6, 0.12.10]'

    }
}

I added it to pubspeck.yml:

onesignal_flutter:2.6.1

this is my main:

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(new MyApp());
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


  @override
  Widget build(BuildContext context) {
    OneSignal.shared.init(
      "id do one signal",
      iOSSettings: {
        OSiOSSettings.autoPrompt: false,
        OSiOSSettings.inAppLaunchUrl: false,
      },
    );
    OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);

    OneSignal.shared.getPermissionSubscriptionState().then((status) {
      UserModel.of(context).playerId = status.subscriptionStatus.userId;
    });
    return ScopedModel<UserModel>(
      model: UserModel(),
      child: MaterialApp(
        title: "ARPA3I",
        theme: ThemeData(
            primarySwatch: Colors.blue,
            primaryColor: Color.fromARGB(255, 4, 125, 141)
        ),
        home: Splash(),
          debugShowCheckedModeBanner: false,
          routes: {
          'LoginScreen': (context) => LoginScreen(),
      },
      ),
    );
  }
}```

 

I need notifications to be sent to my app every time the variable in firebase equals 1 (it gets 1 and 0 when the sensor is triggered gets 1 while the sensor is turned off is set to 0),for this I created this if that takes the value of the sensor in firebase and creates this notification when it is equal to 1, but it is only working while the app is open when it is closed and in the background I do not receive notifications, this is the code:

      Widget build(BuildContext context) {
    return StreamBuilder(
          stream: _btRef.onValue,
          builder: (context, snapshot) {
            var _bt = Botao_pan.fromJson(snapshot.data.snapshot.value['Variaveis']);
            print("Bt: ${_bt.botao_pan} ");
            if (snapshot.hasData && !snapshot.hasError && _bt.botao_pan == 1) {
              btnnotify();
              return AnimatedContainer(
                  height: 160.0,
                  width: 160.0,
                  duration: Duration(seconds: 3),....

btnnotify() async{
   await OneSignal.shared.postNotification(
     OSCreateNotification(
       playerIds: ['player id qualquer'],
       content: "O botão foi pressionado!",
       heading: "Alerta"
     ),
   );
 }   

1 answer

-1

Good morning,

In your code, the application itself checks whether Firebase is 0 or 1 and sends the notification, would that?

If it is, the code is probably not running when it is closed/in the background so it does not issue the notification.

My suggestion is to do this back-end check, so when it is 1 it sends the notification to the app, so it will be displayed even in the background or with the app closed, just make a request, for example:

{   "app_id": "Chave One Signal",   "subtitle": {"en": "Subtitulo"},   "headings": {"en": "Aplicativo 1"},   "include_player_ids": ["Player Id do Dispositivo"],   "contents": {"en": "Mensagem de notificação..."} }

https://documentation.onesignal.com/docs/using-postman

https://documentation.onesignal.com/reference/create-notification

Browser other questions tagged

You are not signed in. Login or sign up in order to post.