display firebase notification in listview

2019-08-21 09:19发布

I have successfully sent Firebase notifications using my own server to android app and I am listing the messages in a listview in a new activity on clicking the notification. My issue is: the notifications display null, the message and title are not displayed in list. issue is notifications display null

FcmMessagingService:

private Map<String, String> data;
private static final String TAG="MyFirebaseMsgService";
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@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);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
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);
    Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher_background);
    Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
    bigpicture.bigPicture(bmp);
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(title)
            //.setContentText(messageBody)
            .setLargeIcon(bmp)
            .setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setContentText(messageBody).setLights(Color.YELLOW, 300, 300)
            .setVibrate(new long[] { 100, 250 })
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

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

class RecyclerViewAdapter:

private List<lst> list;
Context context;
private int lastPosition = -1;
public RecyclerViewAdapter(Context context, List<lst> list) {
    this.list = list;
    this.context = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
    CustomViewHolder viewHolder = new CustomViewHolder(view);
    return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    lst s = list.get(i);
    customViewHolder.title.setText(s.getTitle());
    customViewHolder.message.setText(s.getmessage());
    setAnimation(customViewHolder.itemView, i);
}
private void setAnimation(View viewToAnimate, int position) {
    if (position > lastPosition) {
        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        animation.setDuration(500);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}
@Override
public void onViewDetachedFromWindow(CustomViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    holder.itemView.clearAnimation();
}
@Override
public int getItemViewType(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public int getItemCount() {
    return (null != list ? list.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected TextView title,message;
    public CustomViewHolder(View itemView) {
        super(itemView);
        this.title = itemView.findViewById(R.id.title);
        this.message = itemView.findViewById(R.id.message);
        this.itemView.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v == itemView) {
        }
    }
}

MainActivity:

private List<lst> itemsList = new ArrayList<lst>();
private RecyclerViewAdapter adapter;
RecyclerView recyclerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerview = findViewById(R.id.list);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerview.setLayoutManager(linearLayoutManager);
    adapter = new RecyclerViewAdapter(this, itemsList);
    recyclerview.setAdapter(adapter);
    Set<String> noti_set = PreferenceManager.getDefaultSharedPreferences(this).getStringSet("noti_set", new HashSet<String>());
    for (String noti : noti_set){
        String[] notification = noti.split("---");
        String title = notification[0];
        String message = notification[1];
        lst ls = new lst();
        ls.setMessage(message);
        ls.setTitle(title);
        itemsList.add(ls);
        adapter.notifyDataSetChanged();
    }
}

0条回答
登录 后发表回答