应明确实施如何使用Firebase通知和数据.我读了很多答案,但似乎无法使其发挥作用.这是我的步骤:
1.)我在PHP中将通知和数据传递给android,它看起来很好:
$msg = array
(
"body" => $body,
"title" => $title,
"sound" => "mySound"
);
$data = array
(
"user_id" => $res_id,
"date" => $date,
"hal_id" => $hal_id,
"M_view" => $M_view
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg,
'data' => $data
);
$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( …Run Code Online (Sandbox Code Playgroud) notifications android firebase firebase-realtime-database firebase-cloud-messaging
我正在使用firebase向我的应用发送通知.我想将每台设备上收到的通知保存在本地数据库中.问题是,当应用程序在后台并收到通知时,onMessageReceived()将不会调用该方法,而是根据文档将通知添加到系统托盘中.该文档还提到以下内容,以便在应用程序处于后台时收到通知:
在这些情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递.
因此,当我尝试访问intent中的extras时,找不到通知的键.我的代码在MainActivity.java:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我们从服务器发送的通知:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何检索通知中的值?
android android-intent firebase google-cloud-messaging firebase-cloud-messaging
我已经尝试了这3天的android通知工作,用户点击通知然后活动打开.但每次我举杯,它都说是空的.尝试使用SOF的一些解决方案,但不起作用.你能看看代码有什么问题吗?提前致谢.
通知代码是
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
int notificationId = new Random().nextInt();
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext(),"Default");
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("fromNotification", true);
// Sets the Activity to start in a new, empty task …Run Code Online (Sandbox Code Playgroud) 我正在使用Firebase云消息传递API创建应用程序...我能够从服务器向我的客户端应用程序发送通知和数据.但问题是当应用程序打开时,通知不会在数据出现时触发(我的意思是我记录了它)这不是问题.但是当应用程序关闭时,会收到通知,而我点击通知时会打开活动,而我无法看到日志数据.我需要将数据更新为TextView ..
我的MyFirebaseMessagingService:
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
Log.e("FROM", "From: " + remoteMessage.getFrom());
String data = remoteMessage.getData().get("message");
Log.e("KOIII", "Notification Message Body: …Run Code Online (Sandbox Code Playgroud) 我花了几个小时研究这个但没有运气,所以我在这里问。我已经检查过这些问题但无济于事:
\n\n\n\n直到今天(AFAIK \xe2\x80\x94 我已经有一段时间没有测试它了),Firebase Cloud Messaging 工作得很好。我可以从 Firebase 控制台发送包含数据的通知,并且使用getIntent().getExtras()将允许我从应用程序访问该数据。
然而,今天我发送了一条测试通知,但点击它并没有执行预期的操作。经过一番挖掘,我发现无论如何getIntent().getExtras()都会返回。null这是相关代码:
private void respondToNotificationClick() {\n if (getIntent().getExtras() != null) {\n Log.e("NOTIF", "NOTIF");\n //...\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nonCreate()(此方法在主活动和主活动中调用onResume()。)
但该日志永远不会被打印,如果我尝试在语句Intent.getExtras()之外记录if,我会得到一个 NPE。
我怀疑这与 Firebase 11 与 10 有关,或者这个应用程序的目标是 API 26,但我就是不知道如何修复它,而且 Google 的文档并不总是最有帮助的。
\n\n这里发生了什么?这是一个已知问题吗?是因为我正在使用测试版 API(尽管它应该已经最终确定)?
\n