Android 即使应用程序未运行,在recyclerview中添加新项目时也会显示通知
我想在recyclerview中插入新项目时显示通知,即使应用程序是否正在运行 我试过:Android 即使应用程序未运行,在recyclerview中添加新项目时也会显示通知,android,android-recyclerview,Android,Android Recyclerview,我想在recyclerview中插入新项目时显示通知,即使应用程序是否正在运行 我试过: RecyclerAdapter recyclerAdapter = new RecyclerAdapter(getActivity(), title, date, content); recyclerView.setAdapter(recyclerAdapter); int newCount = recyclerAdapter.getItemCount(); int previousItem = getF
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(getActivity(), title, date, content);
recyclerView.setAdapter(recyclerAdapter);
int newCount = recyclerAdapter.getItemCount();
int previousItem = getFromSavePref();
if(previousItem < newCount)
{
updateNotification();
previousItem=newCount;
savedInSharedPref(previousItem);
}
只有当应用程序正在运行时才会出现通知,即使应用程序未运行,我也要显示通知
如果我在正确的位置调用了updateNotification(),或者需要在其他地方调用它,请告诉我。如果您的应用程序未运行,则不会有可用于添加您的项目的recylcerview;-)那个么我应该怎么做呢?因为您必须在后台使用服务以一定的间隔运行webservice。如果您获得新数据,则可以推送您的通知。hi@Akash在我的代码中,我应该在哪里使用服务?我是否需要BroadcastReceiver?如果可能,请提供一些示例如果您的应用程序未运行,则不会有可用于添加您的项目的recylcerview;-)那个么我应该怎么做呢?因为您必须在后台使用服务以一定的间隔运行webservice。如果您获得新数据,则可以推送您的通知。hi@Akash在我的代码中我应该在哪里使用服务?我是否需要BroadcastReceiver?如果可能,请提供一些示例
public void updateNotification(){
NotificationCompat.Builder notification = new NotificationCompat.Builder(getActivity());
notification.setSmallIcon(R.drawable.ic_launcher);
notification.setContentTitle(getString(R.string.update));
notification.setContentText(getString(R.string.notificatiocontent));
Intent intent = new Intent(getActivity(), Main.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getActivity());
stackBuilder.addParentStack(Main.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, newsNotification.build());
}