本文介绍了Firebase显示成功,但未发送云消息(Android-PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经做了很多研究,以找出导致问题的原因,但是我仍然无法弄清原因.I've done a lot of research to figure out why it's causing the problem, but I still can not figure out why.我可以从控制台发送通知.我可以使用控制台中的令牌将通知发送到一台设备.I can send notifications from console. I can send notifications to one device using token from console.但是我无法从PHP发送云消息.But i couldn't send cloud messaging from PHP.我还编辑了build.gradle并添加了给定Firebase的google-services.json.Also i edited build.gradle and add google-services.json given firebase. Android清单 <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.agebilisim.clooger"> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- [START fcm_default_icon] --> <!-- Set custom default icon. This is used when no icon is set for incoming notification messages. See README(...) for more. --> <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. See README() for more. --> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorAccent" /> <!-- [END fcm_default_icon] --> <activity android:name=".MainActivity" android:theme="@style/AppTheme.NoActionBar" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".cihazlisteleme" android:label="Cihazlarınız" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.CIHAZLISTELEME" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".tamara" android:screenOrientation="portrait" android:label="Cihaz Ekleyin"> <intent-filter> <action android:name="android.intent.action.TARAMA" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".cihaz_kayit" android:screenOrientation="portrait" android:label="Cihazınızı Tanımlayın"> <intent-filter> <action android:name="android.intent.action.CIHAZ_KAYIT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".sensor_gorunum" android:screenOrientation="portrait" android:label="@string/title_activity_sensor_gorunum" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.SENSORGORUNUM" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".grafik" android:screenOrientation="landscape" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.GRAFIK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".ayarkayit" android:label="Sensör Ayarları" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.AYAR_KAYIT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".genel_ayarlar" android:label="Terminal Ayarları" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.GENEL_AYARLAR" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".CONNECT_WIFI" android:label="Clogger Wifi Yapılandırma" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.CONNECT_WIFI" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".CLOGGER_BAGLANTI" android:label="Clogger Wifi Yapılandırma" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.CLOGGER_BAGLANTI" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".CLOGGER_BAGLANTI_SUNUM" android:theme="@style/AppTheme.NoActionBar" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.CLOGGER_BAGLANTI_SUNUM" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".TABLO_BUGUN" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.TABLO_BUGUN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <!-- [START firebase_service] --> <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <!-- [END firebase_service] --> <!-- [START firebase_iid_service] --> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> <!-- [END firebase_iid_service] --> <activity android:name=".ALARM" android:screenOrientation="portrait"></activity> </application></manifest> MyFirebaseMessagingService.javaMyFirebaseMessagingService.javapublic class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; /** * Called when message is received. * * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. */ // [START receive_message] @Override public void onMessageReceived(RemoteMessage remoteMessage) { // [START_EXCLUDE] // There are two types of messages data messages and notification messages. Data messages are handled // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app // is in the foreground. When the app is in the background an automatically generated notification is displayed. // When the user taps on the notification they are returned to the app. Messages containing both notification // and data payloads are treated as notification messages. The Firebase console always sends notification // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options // [END_EXCLUDE] // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. } // [END receive_message] /** * Create and show a simple notification containing the received FCM message. * * @param messageBody FCM message body received. */ private void sendNotification(String messageBody) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.id.action_settings) .setContentTitle("FCM Message") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); }} MyFirebaseInstanceIDService.javaMyFirebaseInstanceIDService.java public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{ String url_goster = "http://185.126.217.71/firebase.php"; RequestQueue requestQueue; private static final String TAG = "MyFirebaseIIDService"; @Override public void onTokenRefresh() { String token = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Token: " + token); sendRegistrationToServer(token); } private void sendRegistrationToServer(final String token) { // token'ı servise gönderme işlemlerini bu methodda yapmalısınız requestQueue= Volley.newRequestQueue(getApplicationContext()); //İNTERNETE BAĞLANIP BİLGİLERİ ÇEKİYORUZ StringRequest request= new StringRequest(Request.Method.POST, url_goster, new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> params = new HashMap<String,String>(); params.put("FIREBASE",token); return params; } }; requestQueue.add(request); }}我尝试了两种不同的PHP代码.I tried two different PHP code.首先,我使用以下代码:First I used this code:<?php// Call .php?Action=M&t=title&m=message&r=token$action=$_GET["Action"];switch ($action) { Case "M": $r=$_GET["r"]; $t=$_GET["t"]; $m=$_GET["m"]; $j=json_decode(notify($r, $t, $m)); $succ=0; $fail=0; $succ=$j->{'success'}; $fail=$j->{'failure'}; print "Success: " . $succ . "<br>"; print "Fail : " . $fail . "<br>"; break;default: print json_encode ("Error: Function not defined ->" . $action);}function notify ($r, $t, $m) { // API access key from Google API's Console if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'i write my firebase server key' ); $tokenarray = array($r); // prep the bundle $msg = array ( 'title' => $t, 'message' => $m, 'MyKey1' => 'MyData1', 'MyKey2' => 'MyData2', ); $fields = array ( 'registration_ids' => $tokenarray, 'data' => $msg ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch,CURLOPT_URL, 'fcm.googleapis.com/fcm/send' ); curl_setopt( $ch,CURLOPT_POST, true ); curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); $result = curl_exec($ch ); curl_close( $ch ); return $result; }?>如果我使用此代码,输出为:if I use this code output is:还有另一个PHP代码:And another PHP code: <?php// API access key from Google API's Consoledefine( 'API_ACCESS_KEY', 'AAAAIYUM_dc:APA91bHkh9tuO47OPhPUTfzjMc7tc6Y6vzDNMbxFeOD0sEkEcTXu5Mtiec_er_k5HIWGf8COysQ1xJ071dJEDb0GA4RKoiuKkd0j-nkY3PRcy8fJ-ZuyPsmGetHmZoT4eL6KQ_GNaU0S' );$registrationIds = array( $_GET['id'] );// prep the bundle$msg = array( 'message' => 'here is a message. message', 'title' => 'This is a title. title', 'subtitle' => 'This is a subtitle. subtitle', 'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 'vibrate' => 1, 'sound' => 1, 'largeIcon' => 'large_icon', 'smallIcon' => 'small_icon');$fields = array( 'registration_ids' => $registrationIds, 'data' => $msg);$headers = array( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json');$ch = curl_init();curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );curl_setopt( $ch,CURLOPT_POST, true );curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );$result = curl_exec($ch );curl_close( $ch );echo $result;此代码输出为:代码显示成功,但我的手机(Lg G3 Android 6.0)无法收到任何通知Code showing success but i couldn't get any notification on my phone (Lg G3 Android 6.0)我从2天开始搜索,仍然不知道是什么问题:(I am searching from 2 days and still do not know what the problem is :(和调试控制台: $ adb shell am start -n "com.agebilisim.clooger/com.agebilisim.clooger.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -DConnecting to com.agebilisim.cloogerConnected to the target VM, address: 'localhost:8600', transport: 'socket'I/System.out: waiting for debugger to settle...I/System.out: waiting for debugger to settle...I/System.out: debugger has settled (1309)W/System: ClassLoader referenced unknown path: /data/app/com.agebilisim.clooger-2/lib/armI/InstantRun: Instant Run Runtime started. Android package is com.agebilisim.clooger, real application class is null.W/System: ClassLoader referenced unknown path: /data/app/com.agebilisim.clooger-2/lib/armD/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.W/InstanceID/Rpc: Found 10005D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.I/FA: App measurement is starting up, version: 10298I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSEV/FA: Collection enabledV/FA: App package, google app id: com.agebilisim.clooger, 1:143966141911:android:a4514eaaaa81f5b0I/FA: To enable faster debug mode event logging run: adb shell setprop debug.firebase.analytics.app com.agebilisim.cloogerD/FA: Debug-level message logging enabledV/FA: Registered activity lifecycle callbackI/FirebaseInitProvider: FirebaseApp initialization successfulV/FA: Checking service availabilityV/FA: Service availableV/FA: Setting useService: trueV/FA: Using measurement serviceV/FA: Connecting to remote serviceW/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.DrawableV/FA: onActivityCreatedD/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xfff5f5f5I/PhoneWindow: [setLGNavigationBarColor] color=0x fff5f5f5I/art: Background sticky concurrent mark sweep GC freed 83588(6MB) AllocSpace objects, 38(856KB) LOS objects, 34% free, 37MB/57MB, paused 519us total 113.859msV/FA: Using measurement serviceV/FA: Connection attempt already in progressV/FA: Activity resumed, time: 47042510D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: trueD/PhoneWindow: notifyNavigationBarColor, color=0x: fff5f5f5, token: android.view.ViewRootImplAO$WEx@2eb93a0I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (I49f5588d88) OpenGL ES Shader Compiler Version: E031.29.00.00 Build Date: 12/19/15 Sat Local Branch: mybranch17333385 Remote Branch: quic/LA.BF.1.1.3_rb1.6 Local Patches: NONE Reconstruct Branch: NOTHINGI/OpenGLRenderer: Initialized EGL, version 1.4V/RenderScript: 0x99516000 Launching thread(s), CPUs 4D/FA: Connected to remote serviceV/FA: Processing queued up service tasks: 2V/ViewRootImpl: Contents drawing finished : com.agebilisim.clooger/com.agebilisim.clooger.MainActivityI/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9e2026e time:25238089D/MyFirebaseIIDService: Token: cds2SIwJXMU:APA91bHAxP479sLz6pCdt2zWXoscfxCNaf6rNG2nE2bItaXoDjd_V_o_zYVQ5iHWSYWrztVUuH76tFL_h00u8iaD5Di1pKEdLTPhhYqaIIjjENaQJBpFrAAhaeej3bcdK0pbEYY-Z4jDW/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jarV/FA: Inactivity, disconnecting from the serviceV/FA: Session started, time: 47052536I/FA: Tag Manager is not found and thus will not be usedD/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=MainActivity, _si=-7538718783108208390}]V/FA: Using measurement serviceV/FA: Connecting to remote serviceD/FA: Connected to remote serviceV/FA: Processing queued up service tasks: 1V/FA: Inactivity, disconnecting from the serviceI/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWNI/ViewRootImpl: ViewRoot's Touch Event : ACTION_UPD/MyFirebaseMsgService: From: 143966141911D/MyFirebaseMsgService: Message data payload: {subtitle=This is a subtitle. subtitle, smallIcon=small_icon, sound=1, title=This is a title. title, vibrate=1, largeIcon=large_icon, message=here is a message. message, tickerText=Ticker text here...Ticker text here...Ticker text here}推荐答案日志显示您收到了以下消息:The logs show that you received the message:D/MyFirebaseMsgService: From: 143966141911D/MyFirebaseMsgService: Message data payload: {subtitle=This is a subtitle. subtitle, smallIcon=small_icon, sound=1, title=This is a title. title, vibrate=1, largeIcon=large_icon, message=here is a message. message, tickerText=Ticker text here...Ticker text here...Ticker text here}您似乎正在寻找要显示在通知托盘中的通知.只需在if语句内调用sendNotification()以获得data有效负载(因为这就是您要发送的内容),就像这样:It looks like you were looking for the notification to appear in the Notification Tray. Just call your sendNotification() inside your if statement for a data payload (since that is what you're sending), like so:if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); sendNotification(remoteMessage.getData().get("message");} 这篇关于Firebase显示成功,但未发送云消息(Android-PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 01:16