将FCM推送通知另存为片段中的消息

时间:2019-01-28 04:33:10

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

这是我的第一个问题。我已将我的域连接到Firebase。当我更新Wordpress博客中的帖子时,推送通知正在工作。好的,那对现在来说很好。但是,我想将所有通知保存在一个Fragment中,并且可以从那里单击以启动帖子ID的活动。如何保存该通知? 这是我的Firebase Messaging代码:

private static int VIBRATION_TIME = 500; // in millisecond
private SharedPref sharedPref;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sharedPref = new SharedPref(this);
    if (sharedPref.getNotification()) {
        // play vibration
        if (sharedPref.getVibration()) {
            ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(VIBRATION_TIME);
        }
        RingtoneManager.getRingtone(this, Uri.parse(sharedPref.getRingtone())).play();

        if (remoteMessage.getData().size() > 0) {
            Map<String, String> data = remoteMessage.getData();
            FcmNotif fcmNotif = new FcmNotif();
            fcmNotif.setTitle(data.get("title"));
            fcmNotif.setContent(data.get("content"));
            fcmNotif.setPost_id(Integer.parseInt(data.get("post_id")));
            displayNotificationIntent(fcmNotif);
        }
    }
}

private void displayNotificationIntent(FcmNotif fcmNotif) {
    Intent intent = new Intent(this, ActivitySplash.class);
    if (fcmNotif.getPost_id() != -1) {
        intent = new Intent(this, ActivityPostDetails.class);
        Post post = new Post();
        post.title = fcmNotif.getTitle();
        post.id = fcmNotif.getPost_id();
        boolean from_notif = !ActivityMain.active;
        intent.putExtra(ActivityPostDetails.EXTRA_OBJC, post);
        intent.putExtra(ActivityPostDetails.EXTRA_NOTIF, from_notif);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(fcmNotif.getTitle());
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(fcmNotif.getContent()));
    builder.setContentText(fcmNotif.getContent());
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setDefaults(Notification.DEFAULT_LIGHTS);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        builder.setPriority(Notification.PRIORITY_HIGH);
    }
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int unique_id = (int) System.currentTimeMillis();
    notificationManager.notify(unique_id, builder.build());
}

}

2 个答案:

答案 0 :(得分:1)

是的,您可以将您的Notification有效负载存储在数据库中,并在片段中创建列表并显示数据库中的数据,并通过按位置获取帖子ID来根据帖子ID单击任何行开始活动

答案 1 :(得分:0)

您可以在通知到达内部时保存通知消息

  

onMessageReceived(RemoteMessage remoteMessage)

这种方法

完整的通知示例是这样的。

with nlp.disable_pipes(*other_pipes):
    nlp.vocab.vectors.name = 'blank_vector'
    optimizer = nlp.begin_training()
    for i in range(10):
        random.shuffle(TRAIN_DATA)
        losses = {}
        batches = get_batches(TRAIN_DATA, 'ner')
        for batch in batches:
            texts, annotations = zip(*batch)
            nlp.update(texts, annotations, losses=losses, drop=0.1, sgd=optimizer)
            print('Losses:', losses)
nlp.to_disk('model')