• Hello Murali,

    I’m trying to push notifications to my flutter mobile app and I’ve read the article you wrote about mobile apps but I want to know what the key should be I’m sending the token within the body of the post request. Like:

    body = {
    “??” : encryptedToken
    }

    what should i replace the “??” with?, and after I send the token should it appear in the device tokens tab in the plugin page?

    Thanks,
    Mostafa

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Murali

    (@murali-indiacitys)

    I will send you sample flutter code from my github library, flutter code to send encrypted push notification to this WordPress PNFPB plugin is like mentioned as below, where strpwd is secret key and we need to follow encrypter to encrypt token using AES SHA 256 encryption logic in flutter and send it using REST Api of this plugin to wordpress. I will send you full sample code in flutter from my github library (which you asked yesterday)

    Following is flutter code sample logic regarding encryption, if you find any other equivalent encryption for AES SHA 256, you can also use it to encrypt it

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    import 'package:firebase_messaging/firebase_messaging.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:connectivity/connectivity.dart';
    import 'package:page_transition/page_transition.dart';
    import 'package:animated_splash_screen/animated_splash_screen.dart';
    import 'package:wp_notify/models/responses/WPStoreTokenResponse.dart';
    import 'package:wp_notify/models/responses/WPUpdateTokenResponse.dart';
    import 'package:wp_notify/wp_notify.dart';
    import 'package:encrypt/encrypt.dart' as mamadem;
    import 'package:http/http.dart' as http;
    import 'package:crypto/crypto.dart' as CryptoPack;

    .....
    String strPwd = "6309094dd17babb4484a59312188075f";
    final key = mamadem.Key.fromUtf8(strPwd);
    final iv = mamadem.IV.fromLength(16);

    //final encrypter = mamadem.Encrypter(mamadem.AES(key));


    final encrypter = mamadem.Encrypter(
    mamadem.AES(key, mode: mamadem.AESMode.cbc));

    final encrypted = encrypter.encrypt(token!, iv: iv);
    //var hmacSha256 = Hmac(
    // sha256, utf8.encode(strPwd)); // HMAC-SHA256
    // var hmacstring =
    // hmacSha256.convert(utf8.encode(token.toString()));

    var hmacSha256 = CryptoPack.Hmac(
    CryptoPack.sha256, ConvertPack.utf8.encode(strPwd)); // HMAC-SHA256

    var hmacstring =
    hmacSha256.convert(ConvertPack.utf8.encode(token.toString()));

    var encryptedsubscription = encrypted.base64 + ":" + iv.base64 + ":" + hmacstring.toString() + ":" + hmacstring.toString();
    var body = {
    'token': encryptedsubscription,
    'userid': '5555'
    };

    var responseOfAPI = await http.post(
    Uri.parse(
    'https://sample.com/wp-json/PNFPBpush/v1/subscriptiontoken'),
    body: body,
    );

    print('Response status: ${responseOfAPI.statusCode}');
    print('Response body: ${responseOfAPI.body}');
    Thread Starter mostafa9999

    (@mostafa9999)

    Hello Murali,

    Thanks for replying, I’ve used the above example with my generated secret key in the mobile app tab in the plugin page and the request returns (200) but when i test it through the push one time tab in the plugin page i receive nothing on my android phone, and there is no device token added to the device tokens on the plugin page, also is there a specific token i should use in my code or just random generated token.

    Thread Starter mostafa9999

    (@mostafa9999)

    —update—
    – the device token has been added but i still receive no notification on my android phone.

    Plugin Author Murali

    (@murali-indiacitys)

    Did you created android app in firebase console under same project where web app credentials are created. After creating android app in firebase console, please download google-services.json integrate in your android app as mentioned in firebase android app integration and also make sure you have valid sha1-fingerprint in firebase console android app to allow firebase notification in android app. sha1-fingerprint for debug can be generated from android studio or whatever tool you are using. google-services.json needs to be downloaded after generating sha1-fingerprint in firebase console and integrate it in your android project (please refer flutter firebase integrations help or search in google you will get instructions), since this is related to android app integration with wordpress pnfpb plugin, please contact me at support email id murali@indiacitys.com or from github i will provide more details.

    Thread Starter mostafa9999

    (@mostafa9999)

    it works fine now i receive the notification when the app in (foreground, background, terminated),

    But the issue is that nothing happens when I click on the notification in the background or terminated mode. However, I’m using the OnMessageOpenedApp for background status and GetInitialMessage for terminated status, the OnMessage works fine when the app is in the foreground.

    Plugin Author Murali

    (@murali-indiacitys)

    I think recent upgraded version of firebase/flutterfire has some issues in background message handling in android app, Please refer github link of flutter fire https://github.com/firebase/flutterfire/discussions/8273#discussioncomment-5174855

    Please try to use below code for push notifications(from this plugin) in flutter for android , if below solution does not work there is another way, i will add extra parameter in this plugin php code while sending httpv1 notification by adding data object with click_action as described in another solution in same github page https://github.com/firebase/flutterfire/discussions/8273#discussioncomment-4548120 – this solution requires code change in this plugin, next week i am planning new release, i will add this parameter exclusively for flutter users. Before that, please try below code as shown below,

    https://github.com/firebase/flutterfire/discussions/8273#discussioncomment-5174855
    First in method to show push notification add reader of a payload.

    void showFlutterNotification(RemoteMessage message) {
    RemoteNotification? notification = message.notification;
    AndroidNotification? android = message.notification?.android;
    if (notification != null && android != null && !kIsWeb) {
    flutterLocalNotificationsPlugin.show(
    notification.hashCode,
    notification.title,
    notification.body,
    NotificationDetails(
    android: AndroidNotificationDetails(
    channel.id,
    channel.name,
    channelDescription: channel.description,
    // TODO add a proper drawable resource to android, for now using
    // one that already exists in example app.
    icon: 'launch_background',
    ),
    ),
    // Add reading of a payload based on the contents of notification
    // In my case:
    payload: notification.data['params']);
    }
    }
    ----------------------------------------------
    Then somewhere in the application for example in initState method try to call the payload.

    NotificationAppLaunchDetails? notificationAppLaunchDetails = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
    if (notificationAppLaunchDetails != null && notificationAppLaunchDetails.didNotificationLaunchApp) {
    var payload = notificationAppLaunchDetails.notificationResponse?.payload;
    if (payload != null && payload.isNotEmpty) {
    // Your code to handle the payload for routing.
    }
    }
    Thread Starter mostafa9999

    (@mostafa9999)

    Thanks for your quick response i’ve tried the code you provided me and the issue still exists, i’m looking forward to your next update and i hope it fixes the problem.

    Plugin Author Murali

    (@murali-indiacitys)

    I have released version 2.00 and in this version, I have included data object as described in below link, you will get data object with”click_action”: “FLUTTER_NOTIFICATION_CLICK”, as described in this link

    https://github.com/firebase/flutterfire/discussions/8273#discussioncomment-4548120

     DATA='{ "notification": { "body": "this is a body", "title": "this is a title"         }, "data": { "att1": "value..",  "att2": "value..",         "click_action": "FLUTTER_NOTIFICATION_CLICK", }

    in your manifest just add following code:
    <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.