Closed Mostafakhaled999 closed 1 month ago
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 have attached full sample code in flutter from my github library
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(
'http://sample.com/wp-json/PNFPBpush/v1/subscriptiontoken'),
body: body,
);
print('Response status: ${responseOfAPI.statusCode}'); print('Response body: ${responseOfAPI.body}');
`
Thank you very much
I've followed your article on how to subscribe from a mobile app to get notification, i'm just stuck at the body of the post request what should be the key i'm sending the token with like:
body = { '??': encryptedToken }