在帖子添加

时间:2017-09-19 12:39:15

标签: android android-notifications

我正在创建一个新闻Android应用程序,我想在帖子添加到database.i时发送通知知道Google Firebase控制台,但我想自动发送通知。我有以下代码:

public void sendNotification() {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    Intent intent = new Intent(MainActivity.this, NewsActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle("My Company");
    mBuilder.setContentText("New news received.");
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(001, mBuilder.build());
}

在Apiservice中使用它:

apiService.getNews(new ApiService.OnNewsReceived() {
     @Override
     public void onReceived(List<News> news) {
           sendNotification();
           adsesAdapter = new AdsesAdapter(NewsActivity.this, ) );
           recyclerView.setAdapter(adsesAdapter);
     }
 });

此代码只显示用户在应用程序时的通知...
是否有任何方式显示收到的新闻通知而无需打开应用程序???

0 个答案:

没有答案