0

Recently I`ve migrated from GCM to FCM and past few days I'm struggling to make it work.

Android Apps are receiving notifications from google firebase console but they don't from php server.

This is my PHP server side code:

<?php



define("GOOGLE_API_KEY", Setting::get('browser_key') ? Setting::get('browser_key') : "");


class GCM {

function __construct() {

}


public function send_notification($registatoin_ids, $message) {

    Log::info("GOOGLE_API_KEY".GOOGLE_API_KEY);

    include_once 'const.php';
    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );

    Log::info('***** PUSH MESSAGE ******'.print_r(json_encode($fields),true));

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    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);

    Log::info(print_r($result,true));

    if ($result === FALSE) {
        //die('Curl failed: ' . curl_error($ch));
        Log::error('Curl failed: ' . curl_error($ch));
    }
    else{
        //echo $result;
        Log::error($result);
    }

    // Close connection
    /*curl_close($ch);
     echo $result/*."\n\n".json_encode($fields); */

}

}
?>

This is my const.php

<?php

    define('TEAM','team');

    define('MESSAGE' , 'message');

?>

This is my firebase messanging code:

public class MessagingService extends FirebaseMessagingService {

private static final String TAG = "FCM Message";

public MessagingService() {
    super();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    sendNotification(remoteMessage.getNotification().getBody());

    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification());
}

private void sendNotification(String body) {


    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent= PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("Codegama");
    notificationBuilder.setContentText(body);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSound(defaultSoundUri);
    notificationBuilder.setContentIntent(pendingIntent);

    NotificationManager notificationManager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());




}
}

and this is the error I keep getting in logcat android studio :

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
    at hr.trazim.client.services.MessagingService.onMessageReceived(MessagingService.java:32)
    at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:60)
    at com.google.firebase.iid.zzg.run(Unknown Source:4)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
    at java.lang.Thread.run(Thread.java:919)

2020-01-07 12:08:11.092 1732-29214/? E/ResolverController: No valid NAT64 prefix (101, /0)

I was reading a lot on stackoverflow subjects like this and I couldn't find proper fix/solution. I hope this fix gonna help other devs like me find suitable answers without lossing too much time as I did.

P.S. In case you need my token I haven't migrated it yet but it's working here is the code :

public class InstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.e("FCMToken", refreshedToken);
    saveDeviceToken(refreshedToken);
}

private void saveDeviceToken(String token) {
    UserDataSource.getDataInstance().putDeviceToken(token);
}

}

Thanks in advance.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
DevGuy
  • 123
  • 1
  • 3
  • 10
  • check this link also https://stackoverflow.com/questions/37350268/send-fcm-messages-from-server-side-to-android-device – KuLdip PaTel Jan 07 '20 at 11:30
  • https://stackoverflow.com/a/40256179/7229971 refer this link, I think in android you don't need to change. – KuLdip PaTel Jan 07 '20 at 11:36
  • Yesterday ive tryed all the solutions provided in this post, i couldnt find the fix in it unfortunatley. Ive sid that for your first link ,im checking second now. – DevGuy Jan 07 '20 at 11:37
  • you are getting error in android right? back-end working fine ? – KuLdip PaTel Jan 07 '20 at 11:37
  • Backend sending data, but android cant handle it. Saying null – DevGuy Jan 07 '20 at 11:38
  • try to log `remoteMessage.toString()` in log cat. – KuLdip PaTel Jan 07 '20 at 11:40
  • Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference – DevGuy Jan 07 '20 at 11:43
  • write this line above sendNotification(remoteMessage.getNotification().getBody()); in android `Log.d("Push data", remoteMessage.getData().toString());` check what you get in logcat – KuLdip PaTel Jan 07 '20 at 11:45
  • Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference < his is what i get. – DevGuy Jan 07 '20 at 11:47
  • first comment out this line sendNotification(remoteMessage.getNotification().getBody()); – KuLdip PaTel Jan 07 '20 at 11:47
  • E/Response: {"success":true,"request_id":85,"message":"Peatelj je uno pri!"} – DevGuy Jan 07 '20 at 11:52
  • Happy to see that ? – KuLdip PaTel Jan 07 '20 at 11:53
  • Yeah but still need to make it working. How can i parse this into notification ? – DevGuy Jan 07 '20 at 11:54
  • now try to this one Log.d("Push data", remoteMessage.getData().get("message")); – KuLdip PaTel Jan 07 '20 at 11:55
  • Push data: {"data":{"page_type":"","request_id":"85"},"success":true,"title":"Us","type":5} D/FCM Message: From: 1005368332618 D/FCM Message: Notification Message Body: null – DevGuy Jan 07 '20 at 12:01
  • are you sending any data in message from php? – KuLdip PaTel Jan 07 '20 at 12:03
  • Yes its comming from other files there is 2 apps..This Log.d("Push data", remoteMessage.getData().get("message")); ive tryed yesterday ive found it in other solution ive found on stackoverflow – DevGuy Jan 07 '20 at 12:04
  • previously is working right ? – KuLdip PaTel Jan 07 '20 at 12:05
  • Yes , when it was GCM now endpoint url is FCM. – DevGuy Jan 07 '20 at 12:05
  • Okay then just replace our php code function with https://stackoverflow.com/a/40256179/7229971. you don't need to change in android code. – KuLdip PaTel Jan 07 '20 at 12:06
  • Ive added only part of code 'notification' => array ( "body" => $mess, "title" => "Title", "icon" => "myicon" Now i get all this in notification, i will play with it now and let you know shortly – DevGuy Jan 07 '20 at 12:17

3 Answers3

0

You are getting wrong data. it should be like this. or try to print data first then get from array.

sendNotification(remoteMessage.getData().get("message"));
Log.d("Push data", remoteMessage.getData().toString());
KuLdip PaTel
  • 1,069
  • 7
  • 19
  • Ive tryed and now i got this : FATAL EXCEPTION: Firebase-MessagingService Process: hr.trazim.client, PID: 29556 java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(channel=null pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE) – DevGuy Jan 07 '20 at 11:33
0

try to use this :

@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

public void show_Notification(){

Intent intent=new Intent(getApplicationContext(),MainActivity.class);
String CHANNEL_ID="MYCHANNEL";
NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
        .setContentText("Heading")
        .setContentTitle("subheading")
        .setContentIntent(pendingIntent)
        .addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)
        .setChannelId(CHANNEL_ID)
        .setSmallIcon(android.R.drawable.sym_action_chat)
        .build();

NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,notification);


 }
Diwakar Singh
  • 267
  • 3
  • 12
0

To send Firebase push notification from php server you can use this code.

<?php

function sendMessage($data, $target, $serverKey){
    //FCM api URL
    $rsp = [];
    $url = 'https://fcm.googleapis.com/fcm/send';
    //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
    $server_key = $serverKey;
    $fields = array();
    $fields['data'] = $data;
    if(is_array($target)){
            $fields['registration_ids'] = $target;
        }else{
            $fields['to'] = $target;
    }
    //header with content_type api key
    $headers = array(
        'Content-Type:application/json',
        'Authorization:key='.$server_key
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        //die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);

    //print_r($result);
    return $result;
}

And if you want to update your android code you can follow this post: Firebase push notification

Aneh Thakur
  • 1,072
  • 12
  • 20