当前位置: 首页 > 知识库问答 >
问题:

Firebase显示成功,但未发送云消息(Android-PHP)

姜煌
2023-03-14

我已经做了很多研究来找出为什么它会导致这个问题,但是我仍然不知道为什么。

我可以从控制台发送通知。我可以从控制台使用令牌向一个设备发送通知。

但是我不能从PHP发送云消息

此外,我还编辑了build.gradle并添加了给定firebase的google-services.json。

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.java

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

MyFireBaseInstancedService.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代码。

首先,我使用了以下代码:

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


?>

如果使用此代码,则输出为:

成功:1失败:0

和另一个PHP代码:

    <?php
// API access key from Google API's Console
define( '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)

我从2天开始搜索,仍然不知道问题是什么:(

和调试控制台:

    $ adb shell am start -n "com.agebilisim.clooger/com.agebilisim.clooger.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Connecting to com.agebilisim.clooger
Connected 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/arm
I/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/arm
D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
W/InstanceID/Rpc: Found 10005
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
I/FA: App measurement is starting up, version: 10298
I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
V/FA: Collection enabled
V/FA: App package, google app id: com.agebilisim.clooger, 1:143966141911:android:a4514eaaaa81f5b0
I/FA: To enable faster debug mode event logging run:
        adb shell setprop debug.firebase.analytics.app com.agebilisim.clooger
D/FA: Debug-level message logging enabled
V/FA: Registered activity lifecycle callback
I/FirebaseInitProvider: FirebaseApp initialization successful
V/FA: Checking service availability
V/FA: Service available
V/FA: Setting useService: true
V/FA: Using measurement service
V/FA: Connecting to remote service
W/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.Drawable
V/FA: onActivityCreated
D/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xfff5f5f5
I/PhoneWindow: [setLGNavigationBarColor] color=0x fff5f5f5
I/art: Background sticky concurrent mark sweep GC freed 83588(6MB) AllocSpace objects, 38(856KB) LOS objects, 34% free, 37MB/57MB, paused 519us total 113.859ms
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 47042510
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
D/PhoneWindow: notifyNavigationBarColor, color=0x: fff5f5f5, token: android.view.ViewRootImplAO$WEx@2eb93a0
I/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: NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
V/RenderScript: 0x99516000 Launching thread(s), CPUs 4
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 2
V/ViewRootImpl: Contents drawing finished : com.agebilisim.clooger/com.agebilisim.clooger.MainActivity
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9e2026e time:25238089
D/MyFirebaseIIDService: Token: cds2SIwJXMU:APA91bHAxP479sLz6pCdt2zWXoscfxCNaf6rNG2nE2bItaXoDjd_V_o_zYVQ5iHWSYWrztVUuH76tFL_h00u8iaD5Di1pKEdLTPhhYqaIIjjENaQJBpFrAAhaeej3bcdK0pbEYY-Z4jD
W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
V/FA: Inactivity, disconnecting from the service
V/FA: Session started, time: 47052536
I/FA: Tag Manager is not found and thus will not be used
D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=MainActivity, _si=-7538718783108208390}]
V/FA: Using measurement service
V/FA: Connecting to remote service
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
V/FA: Inactivity, disconnecting from the service
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
D/MyFirebaseMsgService: From: 143966141911
D/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}

共有1个答案

宋涵忍
2023-03-14

日志显示您收到了以下消息:

D/MyFirebaseMsgService: From: 143966141911
D/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(),即可获得数据有效负载(因为这就是您要发送的内容),如下所示:

if (remoteMessage.getData().size() > 0) {
    Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    sendNotification(remoteMessage.getData().get("message");
}
 类似资料:
  • null 当我确实从Firebase云消息发送给自己一条测试消息时,它确实工作正常,并且成功地向每个设备发送了一个推送通知。 例如,以下是 null

  • 按照Firebase开发文档中的说明,我已经实现了一个扩展Firebase MessagingService的服务,并覆盖了onMessageRec的回调。我已经将一条Log消息放在onMessageRec的方法的第一行中。 应用程序在后台运行我没有在logcat中看到日志,但我看到系统中发布了一个通知。 应用程序在前台我既没有看到日志也没有通知在系统托盘 知道这是怎么回事吗? 清单 服务等级

  • 有人知道一种从firebase控制台发送click_action的方法吗? 我只想发送click_action按钮来使用通知,即使应用程序关闭或在后台。

  • 如果一个主题订阅者在向该主题/订阅者发送推送通知时没有在线,那么他/她的消息会丢失还是当他们在线时才收到消息?

  • 我正在尝试为Web设置Firebase云消息传递。我成功地对其进行了正确初始化并获得了令牌: manifest.json与gcm_sender_id 我可以看到我在控制台中得到令牌,所以我试图验证它,并通过邮递员发送我的第一个通知-这里是留档。 发布网址:https://fcm.googleapis.com/v1/projects/PROJECTID/messages:发送授权:无授权 标题 Bo

  • 我刚开始使用Firebase云消息。我建了一个IOS应用来接收推送通知。应用程序运行良好。我从Firebase控制台发送消息,它们会正确显示。 我的问题是:我可以向所有设备发送一条消息吗(就像我可以在控制台中做的那样)?我是的,怎么会这样? 提前道谢!