FCM messages not reaching device when sent from server

Vyshakh

I am trying to implement FCM in my app. Right now, I am able to receive messages when i send them from firebase app console. But when i try to send messages from my server, the message is not getting delivered to the phone. However, I am getting a success status after sending message from the server and it shows delivered to 1 device. Any help would be appreciated.

Thanks.

Manifest File:

</application>

<service
    android:name=".FirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>


<service
    android:name=".FirebaseInstanceIDService">
    <intent-filter>
        <action     android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

</manifest>

Build.gradle app:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.google.firebase:firebase-messaging:9.0.2'
}

apply plugin: 'com.google.gms.google-services'

Build.gradle project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'



    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Code:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    ShowNotification(remoteMessage.getData().get("message"));
}

private void ShowNotification(String message) {

    Intent i = new Intent(this,MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingintent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

   android.support.v4.app.NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Test")
            .setContentText(message)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentIntent(pendingintent);

    NotificationManagerCompat manager =(NotificationManagerCompat) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());

}



}





public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {


    String token = FirebaseInstanceId.getInstance().getToken();
    System.out.println("TOKEN   " + token);
}
}

Server Side Code:

function send_notification ($tokens, $message)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
         'registration_ids' => $tokens,
         'data' => $message
        );
    $headers = array(
        'Authorization: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);
   return $result;
}


$tokens = array(); 
$tokens[] = "***********************************************";

$message = array("message" => " DEEPAK PUSH TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
Vyshakh

I was able to fix the issue. The format in which I was generating json, was wrong. I didnt put the payload under notification tag. Post that change it started working. Thanks a lot guys, for all your suggestions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FCM messages not reaching device when sent from server

From Dev

Post request not reaching server from iOS device

From Dev

How to get data sent from server using fcm if app is in background?

From Dev

FCM: Is the order in which messages are sent preserved?

From Dev

Server/Client errors when too many messages are sent

From Dev

FCM: Displaying emoji or UTF-8 text on iOS that is sent from Android device

From Dev

Reaching a device in my LAN from the Internet

From Dev

Unable to sent request to FCM url from server (same code is working from local pc)

From Dev

Java - Recognise sent-to-one and sent-to-all messages from server, on the client-side

From Dev

Messages not reaching destination queue when using ServerInitializerFactory Netty 4

From Dev

Receive messages from 2 different senders in FCM

From Dev

Server can not decrypt messages sent by Android 4.2

From Dev

JS prototype method disappears when object is sent to client from server

From Dev

How to set a timeout in a UDP client for when a packet is not sent from server?

From Dev

Argument became null when sent from client to server

From Dev

Android GCM server sent but GCM not pushing to device

From Dev

Distinguish between messages sent from CLIENT and other

From Dev

RabbitMQ Consumer not receiving messages sent from Producer

From Dev

Getting messages sent from python KafkaProducer

From Dev

What happens to incoming messages from twilio when server is down

From Dev

Java Socket: how to skip reading line from server when no output is sent from the server

From Dev

Send data to server from android device when app gets in background

From Dev

Pull notifications from web server to iOS device when app is backgrounded

From Java

How to get User- email Id from hangout messages that are being sent to Rasa server after Rasa<->Google chat integration

From Dev

maniplate html sent from server

From Dev

Data sent to and received from server

From Dev

Reaching Node.js server externally from Google Compute Engine

From Dev

Java UDP packets not reaching Server program from Client program

From Dev

Reaching files from a file server created with Raspberry and Samba

Related Related

  1. 1

    FCM messages not reaching device when sent from server

  2. 2

    Post request not reaching server from iOS device

  3. 3

    How to get data sent from server using fcm if app is in background?

  4. 4

    FCM: Is the order in which messages are sent preserved?

  5. 5

    Server/Client errors when too many messages are sent

  6. 6

    FCM: Displaying emoji or UTF-8 text on iOS that is sent from Android device

  7. 7

    Reaching a device in my LAN from the Internet

  8. 8

    Unable to sent request to FCM url from server (same code is working from local pc)

  9. 9

    Java - Recognise sent-to-one and sent-to-all messages from server, on the client-side

  10. 10

    Messages not reaching destination queue when using ServerInitializerFactory Netty 4

  11. 11

    Receive messages from 2 different senders in FCM

  12. 12

    Server can not decrypt messages sent by Android 4.2

  13. 13

    JS prototype method disappears when object is sent to client from server

  14. 14

    How to set a timeout in a UDP client for when a packet is not sent from server?

  15. 15

    Argument became null when sent from client to server

  16. 16

    Android GCM server sent but GCM not pushing to device

  17. 17

    Distinguish between messages sent from CLIENT and other

  18. 18

    RabbitMQ Consumer not receiving messages sent from Producer

  19. 19

    Getting messages sent from python KafkaProducer

  20. 20

    What happens to incoming messages from twilio when server is down

  21. 21

    Java Socket: how to skip reading line from server when no output is sent from the server

  22. 22

    Send data to server from android device when app gets in background

  23. 23

    Pull notifications from web server to iOS device when app is backgrounded

  24. 24

    How to get User- email Id from hangout messages that are being sent to Rasa server after Rasa<->Google chat integration

  25. 25

    maniplate html sent from server

  26. 26

    Data sent to and received from server

  27. 27

    Reaching Node.js server externally from Google Compute Engine

  28. 28

    Java UDP packets not reaching Server program from Client program

  29. 29

    Reaching files from a file server created with Raspberry and Samba

HotTag

Archive