1

我想在我的应用程序中实现FCM。现在,当我从Firebase应用程序控制台发送消息时,我可以收到消息。但是,当我尝试从我的服务器发送消息时,消息不会传送到手机。但是,从服务器发送消息并显示传送到1设备后,我获得了成功状态。任何帮助,将不胜感激。当从服务器发送FCM消息未到达设备

谢谢。

清单文件:

</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应用:

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

发布您的代码/ logcat.You应该给出更多的信息 –

+0

请确保您加入这个**谷歌服务**文件,它包含火力网址'project_info“:{ ‘PROJECT_NUMBER’:‘XXXXXXXXXX’, ” firebase_url“:”https://projectid.firebaseio.com“, ”project_id“:”projectid“, ”storage_bucket“:”projectid.appspot.com“ }'也检查此文件中是否存在其他参数,然后尝试 –

+0

您是否使用本地服务器(如Xampp等)或任何免费的虚拟主机? 如果您可以使用FCM控制台发送消息(Notification),那么您的Android应用程序代码几乎没有问题。到你的服务器数据库和Sc ript发送邮件请求到FCM服务器。我有类似的问题,改变我的PHP代码1000次后,我用我的XAMPP locat主机和相同的代码工作像魅力。问题出在我的免费虚拟主机服务器设置中,而不是我的代码中。可能也是这种情况;) – Raj

回答

0

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

相关问题