在应用程序外部通知通知(android)

时间:2018-04-05 23:49:33

标签: java android android-studio notifications

  • 我有一个ArrayList,其中包含一天中小时的int和一天中某分钟的int。我希望每当当前时间等于arraylist中的时间时发出通知
  • 每当我运行代码时,只有在arraylist中一次启动应用程序时才会显示通知
  • 我希望即使用户不在应用程序中也会显示通知
  • 我认为我的问题是我的通知目前存储在我的onCreate()部分,但是当用户不在应用程序中时,我不知道将它放在何处以显示通知
public class MainActivity extends AppCompatActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
...
int notifyID = 1;
        String CHANNEL_ID = "Class Over";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = "my_channel_name";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mChannel.setDescription("notification channel description");
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(mChannel);
        }
        Calendar rightNow = Calendar.getInstance();
        for(int i=0; i<classes.size(); i++)
        {
            if(rightNow.get(Calendar.HOUR_OF_DAY) == classes.get(i).getClassHour() && rightNow.get(Calendar.MINUTE) == classes.get(i).getClassMin())
            {

                NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("Any Homework for " + classes.get(i).getClassName())
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
                notificationManager.notify(notifyID, notification.build());
            }

        }
}

2 个答案:

答案 0 :(得分:0)

要在android中安排作业,您需要了解作业调度程序或 fireBaseJobDispatcher,您可能还需要broadCast接收器,具体取决于您希望加载通知的条件, 一些有用的链接:) https://developer.android.com/topic/performance/scheduling.html https://developer.android.com/reference/android/content/BroadcastReceiver.html

答案 1 :(得分:0)

请尝试以下代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getNotification() != null) {
        remoteMessage.getNotification();

    }

    if (remoteMessage.getData() != null) {

        Map<String, String> data = remoteMessage.getData();

        String message = data.get("message");

  generateNotification(this,message, false);   
  }
}
}

public void generateNotification(Context context, String message, boolean isSilent) {


    // Message does not null or empty
    if (CommonUtil.isNullString("" + message)) {
        return;
    }


    NotificationManager mNotificationManager;


        Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
                context.getResources(),
                R.drawable.ic_img);


        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
                context).setAutoCancel(true)
                .setContentTitle("" + context.getResources().getString(R.string.app_name))
                .setContentText(message)
                .setColor(context.getResources().getColor(R.color.colorWhite))
                .setSmallIcon(R.drawable.ic_img);
               .setLargeIcon(notificationLargeIconBitmap);



        Intent resultIntent = new Intent(context, ProfileNavigationActivity.class);

        resultIntent.putExtra(Constants.INTENT_NOTIFICATION, Constants.INTENT_NOTIFICATION_VALUE);

        resultIntent.putExtra(Constants.INTENT_NOTIFICATION_MESSAGE, "" + message);

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(),
                resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        mBuilder.setContentText("" + message);
        Notification notification = mBuilder.build();

        mBuilder.setPublicVersion(notification);
        // If isSilent flag is true, does not make sound and vibrate.
        if (!isSilent) {
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }

        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // Notification id allows you to updateFragment the notification later on.
        if (mNotificationManager != null) {
            mNotificationManager.notify(0, notification);
        }
    }