2

I'm trying to send push notifications from server to app using Firebase, but when the notification reaches app, my app is crashing.

PHP code:

       <?php

       $registrationIds = array("USER-DEVICE-TOKEN" );

       $message = array
       (
        'message'   => 'My awesome message',
        'title'     => 'My awesome title',
        'subtitle'  => 'My awesome subtitle',
        'tickerText'    => 'My awesome Ticker text',
        'vibrate'   => 1,
        'sound'     => 1,
        'largeIcon' => 'large_icon',
        'smallIcon' => 'small_icon'
       );


       $url = 'https://fcm.googleapis.com/fcm/send';
       $fields = array(
         'registration_ids' => $registrationIds,
         'data' => $message
        );
       $headers = array(
        'Authorization:key = GOOGLE-API-KEY',
        'Content-Type: application/json'
        );
       $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('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);

       echo $result;

Output of PHP Script

My Firebase messaging service:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); // LINE 24
    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());
}

//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody,String messageTitle) {
    Intent intent = new Intent(this, LoginActivity.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)
            .setSmallIcon(R.drawable.mainlogo)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}
       }

Crashing error when push notification comes

Hope for your help

Yura Rosiak
  • 255
  • 2
  • 13

1 Answers1

3

Check if remoteMessage.getNotification() is not NULL. I think it is NULL because you are not sending the param 'notification' in your $fields array of your PHP code. You are sending 'data'. So, when you send the parameter 'data' you have to get that parameter with remoteMessage.getData().

Try to print the content of remoteMessage.getData().toString() in your onMessageReceived method. It should print the data you are sending in the notification.

Hope this helps.

GeorgeLBA
  • 334
  • 1
  • 3
  • Thank you for help, i've changed "data" to "notification" in my php array, and it works! – Yura Rosiak Aug 19 '16 at 11:53
  • @YuraRosiak I've written same code but its not working. can you hlp plz? – Tashen Jazbi Oct 05 '16 at 09:20
  • @TashenJazbi could you please provide details on the error you are getting?. I mean, Do you receive the notification in the system tray? Do you get the onMessageReceived called?. Are you getting any exception? – GeorgeLBA Oct 06 '16 at 15:02
  • onMessageReceived not being called :( . I've asked question there http://stackoverflow.com/questions/39870556/onmessagereceived-never-called-in-notification-from-server-using-firebase-in-and/39872412#39872412 – Tashen Jazbi Oct 06 '16 at 20:05
  • read this blogpost http://developine.com/how-to-send-firebase-push-notifications-from-app-server-tutorial/ – Developine Nov 11 '17 at 15:37