【问题标题】:Display FCM notification message in Listview onClicking Notification在 Listview 中显示 FCM 通知消息 onClicking Notification
【发布时间】:2017-12-05 15:54:28
【问题描述】:

我已使用自己的服务器成功地将 Firebase 通知发送到 Android 应用程序,我想通过单击通知在新活动中的 ListView 中列出消息。我应该对通知消息做些什么来显示在 ListView 中,我想帮助完成这项工作。

FirebaseMessageservice.java:

    public class FcmMessagingService extends FirebaseMessagingService {
    private Map<String, String> data;
    private static final String TAG="MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        data=remoteMessage.getData();
        String message=data.get("message");
        String titledata=data.get("title");
        ManualNotification(titledata , message);

        Set<String> noti_set = PreferenceManager.getDefaultSharedPreferences(context).getStringSet("noti_set", new HashSet<String>());
        Set<String> set = new HashSet<String>();
        set.add(titledata+"---"+message);
        set.addAll(noti_set);
        PreferenceManager.getDefaultSharedPreferences(context).edit().putStringSet("noti_set", set).apply();

    }
private void ManualNotification(String title , String messageBody){
        Intent intent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("message", messageBody);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        long[] vv = {500,1000};
        Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher);
        Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
        bigpicture.bigPicture(bmp);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setLargeIcon(bmp)
                .setSound(defaultSoundUri)
                .setVibrate(vv)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

FcmInstanceIdService.java:

  public class FcmInstanceIdService extends FirebaseInstanceIdService {
private static final String REG_TOKEN = "REG_TOKEN";
@Override
public void onTokenRefresh() {
    String recent_token= FirebaseInstanceId.getInstance().getToken();
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("FCM_TOKEN", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("FCM_TOKEN",recent_token);
    editor.apply();
    Log.d(REG_TOKEN,recent_token);
}

lst.java:

   public class lst {
private String title;
private String message;

public lst() {
}

public lst(String title, String message) {
    this.title = title;
    this.message = message;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getmessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

} Messages.java:

  public class Messages extends AppCompatActivity {


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

    Set<String> noti_set = PreferenceManager.getDefaultSharedPreferences(context).getStringSet("noti_set", new HashSet<String>());
    for (String noti : noti_set){
            String title = noti.splite("---")[0];
            String message = noti.splite("---")[1];
            list.add(new lst(title,message));
            adapter.notifyDataSetChanged();
    }

}

}

【问题讨论】:

    标签: android listview firebase firebase-cloud-messaging react-native-fcm


    【解决方案1】:

    首先,

    我会推荐你​​使用 RecyclerView 而不是 ListView 来显示通知列表。 更多关于如何开始的信息here

    其次,

    onMessageRecieved(...) 仅在您的应用处于前台时触发/触发,因此如果您的应用处于后台,则会收到通知,但不会将其附加到共享首选项.

    最后

    不是在客户端列出/存储通知,而是在服务器端进行。如果您使用的是 Firebase,我建议您将所有通知放在实时数据库中,并在需要时从那里检索。

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多