Vyshakh
Vyshakh

Reputation: 632

FCM messages not reaching device when sent from server

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;

Upvotes: 4

Views: 4906

Answers (1)

Vyshakh
Vyshakh

Reputation: 632

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.

Upvotes: 2

Related Questions