从通知中获取额外收益-FCM

时间:2018-07-04 01:16:21

标签: android firebase push-notification kotlin firebase-cloud-messaging

当我在单击通知时打开应用程序时,我试图从捆绑销售中获得更多收益。

我在MainActivity上有此方法:

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

2 个答案:

答案 0 :(得分:0)

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

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);

在您的MainActivity中:

@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
        }
    }
}

答案 1 :(得分:0)

如果您在应用中使用SplashActivity,则可以从中获取数据

内部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) {
            }
        }
   }
}