Flutter firebase messaging not receiving notification

Mobile Technologies Mobile Computing 3 years ago

2.35K 1 0 0 0

_x000D_ _x000D_ I currently working on flutter notification using firebase messaging, i couldn't receive any notification. I send the notification via server using curl, however i can get it work in native android app (Android Studio) but not in flutter, any help would be appreciated. Below is my code. Flutter Notification code class FirebaseNotifications { FirebaseMessaging _firebaseMessaging; void setUpFirebase() { _firebaseMessaging = FirebaseMessaging(); firebaseCloudMessaging_Listeners(); } void firebaseCloudMessaging_Listeners() { if (Platform.isIOS) iOS_Permission(); _firebaseMessaging.getToken().then((token) { print(token); }); _firebaseMessaging.configure( onMessage: (Map message) async { print('on message $message'); }, onResume: (Map message) async { print('on resume $message'); }, onLaunch: (Map message) async { print('on launch $message'); }, ); } void iOS_Permission() { _firebaseMessaging.requestNotificationPermissions( IosNotificationSettings(sound: true, badge: true, alert: true)); _firebaseMessaging.onIosSettingsRegistered .listen((IosNotificationSettings settings) { print("Settings registered: $settings"); }); } } Android Studio code @Override public void onMessageReceived(RemoteMessage remoteMessage) { Map params = remoteMessage.getData(); JSONObject object = new JSONObject(params); showNotification(remoteMessage); } private void showNotification(RemoteMessage remoteMessage) { Map data = remoteMessage.getData(); String mesg = "New Notification"; Intent intent; PendingIntent pendingIntent; NotificationCompat.Builder builder; Intent i = new Intent(this,MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(id, name, importance); mChannel.setDescription(description); mChannel.enableLights(true); mChannel.setLightColor(Color.GREEN); mChannel.enableVibration(true); mChannel.setVibrationPattern(new long[]{0, 250, 250, 250}); mNotificationManager.createNotificationChannel(mChannel); builder = new NotificationCompat.Builder(this, id); intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); builder.setContentTitle(data.get("title").toString()) // required //.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification() .getBod y().toString())) .setStyle(new NotificationCompat.BigTextStyle().bigText(mesg)) //custom data .setSmallIcon(R.drawable.ic_stat_notification_icon4) // required .setContentText(mesg) // custom data .setWhen(System.currentTimeMillis()) .setDefaults(Notification.DEFAULT_ALL) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setAutoCancel(true) .setContentIntent(pendingIntent) .setVibrate(new long[]{0, 250, 250, 250}); } } Server side code function sendtoUser($sendingTarget, $acc_name, $type_message, $type_of_transaction, $check_amount) { #API access key from Google API's Console $API_ACCESS_KEY = "adazxc"; $registrationIds = $sendingTarget; #prep the bundle $fields = array( 'to' => $registrationIds, 'data' => array( 'title' => 'my title', 'keyValue' => true, 'receiverName' => $acc_name, 'transType' => $type_of_transaction, 'totalAmount' => $check_amount ), ); $headers = array( 'Authorization: key=' . $API_ACCESS_KEY, 'Content-Type: application/json' ); #Send Reponse To FireBase Server $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); curl_close($ch); } EDIT I somehow can receive notification but i noticed that my status bar not receiving the notification but it is printed in the console, however, when i close the app i can get the notification. So how do i able to receive notification in both situation?

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago
_x000D_ You may miss manual configuration for android (as well as ios if required), refer below link http://myhexaville.com/2018/04/09/flutter-push-notifications-with-firebase-cloud-messaging/
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

Would Firebase be a good choice as a server for the Flutter mobile game I'm creating?

_x000D_ _x000D_ I am building a turn-based Flutter game. Would Firebase be a good choice as a server...
Q

Implement Bottom Navigation activity along with a VerticalViewPager with nested Cardview in Android...

_x000D_ _x000D_ I want to develop an Android App which will have a Bottom Navigation Bar and above i...
Q

(JS) For , determine if the device offers or not to capture a photo from the camera

_x000D_ _x000D_ When uploading a file, I need to give the user the possibility: upload the file fr...

Important Mobile Technologies Links