簡體   English   中英

在Listview onClicking通知中顯示FCM通知消息

[英]Display FCM notification message in Listview onClicking Notification

我已使用自己的服務器成功將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();
    }

}

}

首先,

我建議您使用RecyclerView而不是ListView來顯示通知列表。 有關如何從此處開始的更多信息

其次,

onMessageRecieved(...)僅在您的應用程序位於前景中時才觸發/觸發,因此,如果您的應用程序處於后台,則將收到通知,但不會附加到共享首選項中。

最后

而是在客戶端列出/存儲通知,而在服務器端進行。 如果您使用的是Firebase ,建議您將所有通知放入實時數據庫中,並在需要時從那里檢索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM