不要显示Android通知

时间:2017-07-23 19:17:59

标签: java android notifications

我正在尝试学习如何为Android设备开发应用程序。有一段时间我一直在开发一个应用程序,其中有一个功能,可以在一天中的不同时间发送通知(提醒)。但是如果手机被锁定并且发送通知的时间超过10分钟,那么他们就不会来。尝试过BroadcasrReceiver:

public class NotificationReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    addNotif(context, "Times UP Receiver", "5 second Receiver", "Alert Receiver");
}

private void addNotif(Context context, String msg, String msgText, String msgAlert) {
    Notification.Builder builder = new Notification.Builder(context);
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent)
            .setSmallIcon(R.drawable.ic_android_black_24dp)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp))
            .setTicker(msgAlert)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(msg)
            .setContentText(msgText);

    Notification notification = builder.build();
    notification.defaults = Notification.DEFAULT_ALL;

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(101, notification);
}
}

使用AlarmManager:

        Calendar calNotif = Calendar.getInstance();
    Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 102, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (15 * 60 * 1000), pendingIntent);

我也尝试过使用服务:

public class NotificationService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Calendar calNotif = Calendar.getInstance();
    Intent alertIntent = new Intent(getBaseContext(), NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 103, alertIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager notifAlarm = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis() + (16 * 60 * 1000), pendingIntent);
    return START_STICKY;
}
}

清单:

    <uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".NotificationReceiver" />
    <service android:name=".NotificationService" />

但没有任何作用。我的头疼,我仍然无法理解为什么通知不来。请告诉我,问题是什么。

1 个答案:

答案 0 :(得分:0)

不要使用BaseContext并且您要测试通知,那么为什么要延迟15分钟通知。最大限度地减少时间,并在工作增加时间后进行检查。

在YourActivity中

Calendar calNotif = Calendar.getInstance();
Intent alertIntent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 102, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager notifAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN)
notifAlarm.setExact(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis()+10000, pendingIntent);
else 
notifAlarm.set(AlarmManager.RTC_WAKEUP, calNotif.getTimeInMillis()+10000, pendingIntent);

在您的通知代码中

 builder.setPriority(Notification.PRIORITY_HIGH).setContentIntent(notifIntent)
        .setSmallIcon(R.drawable.ic_android_black_24dp)
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_android_black_24dp))
        .setTicker(msgAlert)
        .setWhen(System.currentTimeMillis())
        .setContentTitle(msg)
        .setContentText(msgText);

删除setLargeIcon,因为它使用Bitmap并占用更多内存。工作完成后,您可以使用服务实现。