简体   繁体   中英

Android notifications not showing

I'm trying to make a daily notification which will be show at a specific time. Unfortunately it doesn't show.

I tried to follow couples tuto (also from developer.android.com ) and checked similar questions that have already been asked. To save hour I'm using Hawk library.

Intent intent = new Intent(getContext(), AlarmReceiver.class);
    int notificationId = 1;
    intent.putExtra("notificationId", notificationId);

PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 0,
            intent,PendingIntent.FLAG_NO_CREATE);

AlarmManager alarm = (AlarmManager)  getContext().getSystemService(getContext().ALARM_SERVICE);


    switch (view.getId()) {
    int hour = timePicker.getCurrentHour();
            int minute = timePicker.getCurrentMinute();

            // Create time
            ....

            //set alarm
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime, AlarmManager.INTERVAL_DAY, alarmIntent);

            Hawk.put("notification_hour", alarmStartTime);

            break;

        case R.id.cancel_button:
//cancel notification
            break;
    }
}

and here AlarmReceiver class

public class AlarmReceiver extends BroadcastReceiver {

    public AlarmReceiver () {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        sendNotification(context, intent);
    }

    private void sendNotification(Context con, Intent intent) {

        int notificationId = intent . getIntExtra ("notificationId", 1);
        String message = " message";

        Intent mainIntent = new Intent(con, MainActivity.class);
        PendingIntent contentIntent = PendingIntent . getActivity (con, 0, mainIntent, 0);

        NotificationManager myNotificationManager =(NotificationManager) con . getSystemService (Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(con);
        builder.setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Reminder")
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_ALL);

        myNotificationManager.notify(notificationId, builder.build());
    }
}

In OREO, they have redesigned notifications to provide an easier and more consistent way to manage notification behavior and settings. Some of these changes include:

Notification channels : Android 8.0 introduces notification channels that allow you to create a user-customizable channel for each type of notification you want to display. Notification dots: Android 8.0 introduces support for displaying dots, or badges, on app launcher icons. Notification dots reflect the presence of notifications that the user has not yet dismissed or acted on.

Snoozing : Users can snooze notifications, which causes them to disappear for a period of time before reappearing. Notifications reappear with the same level of importance they first appeared with.

Messaging style : In Android 8.0, notifications that use the MessagingStyle class display more content in their collapsed form. You should use theMessagingStyle class for notifications that are messaging-related. Here, we have created the NotificationHelper class that require the Context as the constructor params. NOTIFICATION_CHANNEL_ID variable has been initialize in order to set the channel_id to NotificationChannel.

The method createNotification(…) requires title and message parameters in order to set the title and content text of the notification. In order to handle the notification click event we have created the pendingIntent object, that redirect towards SomeOtherActivity.class.

Notification channels allow you to create a user-customizable channel for each type of notification you want to display. So, if the android version is greater or equals to 8.0, we have to create the NotificationChannel object and set it to createNotificationChannel(…) setter property of NotificationManager.

public class NotificationHelper {

private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String NOTIFICATION_CHANNEL_ID = "10001";

public NotificationHelper(Context context) {
    mContext = context;
}

/**
 * Create and push the notification 
 */
public void createNotification(String title, String message)
{    
    /**Creates an explicit intent for an Activity in your app**/
    Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

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

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}

Just include a NotificationChannel and set a channel id to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM