gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:40:33 28 4
gpt4 key购买 nike

我已经做了很多研究来找出它导致问题的原因,但我仍然无法弄清楚原因。

我可以从控制台发送通知。我可以使用来自控制台的 token 向一台设备发送通知。

但我无法从 PHP 发送云消息。

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

Android list

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

MyFirebaseInstanceIDService.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;
}


?>

如果我使用这段代码输出是:

Success: 1 Fail : 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;

此代码输出为:

{"multicast_id":5272616625278801394,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1488877477780922%e3ee78bef9fd7ecd"}]}

代码显示成功,但我无法在我的手机上收到任何通知(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}

最佳答案

日志显示您收到了消息:

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() 获取 data 有效负载(因为这是您要发送的内容),如下所示:

if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("message");
}

关于php - Firebase 显示成功但不发送云消息 (Android - PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42644167/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com