gpt4 book ai didi

android - Firebase (FCM) : open activity and pass data on notification click. 安卓

转载 作者:IT王子 更新时间:2023-10-29 00:06:15 27 4
gpt4 key购买 nike

应该明确实现如何使用 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( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

2.) 在 Android 中收到通知和数据时,它会显示通知。当我单击此通知时,它会打开应用程序。但是我不知道打开应用程序时处理数据的方法。当应用程序处于前台和后台时,存在一些差异。我现在拥有的代码如下:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

String user_id = "0";
String date = "0";
String cal_id = "0";
String M_view = "0";

if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
user_id = remoteMessage.getData().get("user_id");
date = remoteMessage.getData().get("date");
hal_id = remoteMessage.getData().get("hal_id");
M_view = remoteMessage.getData().get("M_view");
}

//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody(), user_id, date, hal_id, M_view);
}

private void sendNotification(String messageBody, String user_id, String date, String hal_id, String M_view) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.putExtra("fcm_notification", "Y");
intent.putExtra("user_id", user_id);
intent.putExtra("date", date);
intent.putExtra("hal_id", hal_id);
intent.putExtra("M_view", M_view);
int uniqueInt = (int) (System.currentTimeMillis() & 0xff);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), uniqueInt, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}}

3.) 当我使用上面的代码并单击通知时,它会在后台打开应用程序。如果应用程序在前台,那么在通知上单击它只会关闭通知。但是,我想在两种场景(后台和前台)中接收数据并打开特定的 Activity 。我在 MainActivity 中有以下代码,但我无法获取数据。 fcm_notification, date, hal_id 返回 null。

public class MainActivity extends Activity {
UserFunctions userFunctions;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
Intent intent_o = getIntent();
}

@Override
protected void onResume() {
super.onResume();
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
Intent intent_o = getIntent();
String fcm_notification = intent_o.getStringExtra("fcm_notification") ;
String user_id = intent_o.getStringExtra("user_id");
String date = intent_o.getStringExtra("date");
String hal_id = intent_o.getStringExtra("hal_id");
String M_view = intent_o.getStringExtra("M_view");
Intent intent = new Intent(this, JobList.class);

// THIS RETURNS NULL, user_id = null
System.out.print("FCM" + user_id);
startActivity(intent);
finish();
}else{
// user is not logged in show login screen
Intent login = new Intent(this, LoginActivity.class);
startActivity(login);
// Closing dashboard screen
finish();
}
}}

如果任何人都可以指导或建议我如何在任一场景(前台或后台)中从 Firebase 检索 MainActivity.java 中的数据,那将是非常棒的。

最佳答案

首先,我将在 Handling Messages docs 中提到的详细信息.

Both行下的摘要中,显示当应用在前台时,payload会在你的onMessageReceived().

为了从 onMessageReceived() 打开 Activity ,您应该检查您需要的数据是否在有效负载中,如果是,请调用您的特定 Activity ,然后传递您需要的所有其他详细信息通过 Intent 。

现在,如果应用程序在 background 中,文档中提到通知由 Android 系统托盘接收,并且可以从中检索 data 有效负载 Intent 的附加部分。

只需从我的回答中添加详细信息 here这几乎只是给出了文档声明和一个示例链接:

Handle notification messages in a backgrounded app

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

我认为 answer @ArthurThompson 解释得很好:

When you send a notification message with a data payload (notification and data) and the app is in the background you can retrieve the data from the extras of the intent that is launched as a result of the user tapping on the notification.

From the FCM sample which launches the MainActivity when the notification is tapped:

if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}

关于android - Firebase (FCM) : open activity and pass data on notification click. 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40181654/

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