我已经使用我自己的服务器成功将Firebase通知发送到Android应用程序,并且我在单击通知的新活动中列出了列表视图中的消息。我的问题是:通知显示为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();
}
}