从通知中获取额外信息 - FCM



当我在单击通知时打开应用程序时,我正在尝试从捆绑包中获取额外的内容。

我在主活动上有这个方法:

private void getBundleFromFirebaseMessaging(){
Intent intent = this.getIntent();
if (intent != null && intent.getExtras() != null) {
Bundle b = getIntent().getExtras();
boolean cameFromNotification = b.getBoolean("fromNotification");
if (cameFromNotification) {
Toast.makeText(this, "FROM PUSH", Toast.LENGTH_SHORT).show();
}
}

我得到的唯一捆绑包是_fbSourceApplicationHasBeenSet。

这是我在FirebaseMessaging 类上的方法

private fun sendNotification(message: RemoteMessage) {
val messageBody = message.notification?.body
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("fromNotification", true)
val pendingIntent = PendingIntent.getActivity(this,
0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = getString(R.string.notification_channel)
val channelName = getString(R.string.notification_channel_name)
val defaultSoundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat
.Builder(this, channelId)
.setSmallIcon(R.drawable.login_logo)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}

这是我之前遇到类似问题时使用的代码。

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Message", message);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,intent ,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

在您的主活动中:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("Message"))
{
// get extras Notification
String msg = extras.getString("Message");
//next you can continue using the msg wherever the MainActivity wants
}
}
}

如果您在应用程序中使用 SplashActivity,那么它是从中获取数据的人

inside onCreate((

if (getIntent().getExtras()!=null){
JSONObject json = new JSONObject();
Set<String> keys = getIntent().getExtras().keySet();
for (String key : keys) {
try {
json.put(key, JSONObject.wrap(extras.get(key)));
} catch (JSONException e) {
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新