从服务器发送时,FCM消息未到达设备

时间:2016-08-09 06:52:50

标签: android push-notification google-cloud-messaging android-notifications firebase-cloud-messaging

我正在尝试在我的应用中实施FCM。现在,当我从firebase应用程序控制台发送消息时,我能够收到消息。但是当我尝试从我的服务器发送消息时,消息没有传送到手机。但是,从服务器发送消息后,我获得了成功状态,并显示已传送到1个设备。任何帮助将不胜感激。

感谢。

清单文件:

</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项目:

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
}

代码:

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

服务器端代码:

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;

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题。我生成json的格式是错误的。我没有把有效负载放在通知标签下。发布改变它开始工作。非常感谢你们的所有建议。