Android 5.1 Notificatios

时间:2018-01-27 15:55:49

标签: java android

对于学校我只需要制作一个简单的应用程序,当我按下按钮时会通知我。

我为活动编写了课程,但谷歌文档是针对Android 8.0的,而不是我的开发版本的android(5.1.1)。

有没有人知道如何发送通知(因为我想我想出了如何制作通知)?

感谢,

伊万

我的班级:

https://gist.github.com/Tebreca/f310b7d921caff04eaddc530521ce824

1 个答案:

答案 0 :(得分:0)

This will set you for life ;)

PendingIntent pIpanel ="你可以这样做:

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
        if (LOG_DEBUG) Log.v(TAG, " : version : <=M ");

        //noinspection deprecation
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_subtext))
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setLights(Color.CYAN, 500, 1200)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(pIpanel)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

        notification = builder.build();



    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N | Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
        if (LOG_DEBUG) Log.v(TAG, " : version : N| N1 - 24: 25 ");

        //noinspection deprecation
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_subtext))
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setLights(Color.CYAN, 500, 1200)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(pIpanel)

                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

        notification = builder.build();


    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (LOG_DEBUG) Log.v(TAG, " : version : >=O ");


        NotificationChannel mChannel = new NotificationChannel
                (NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

        mChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.CYAN);
        mChannel.enableVibration(true);
        notificationManager.createNotificationChannel(mChannel);

        NotificationChannelGroup mGroup = new NotificationChannelGroup(NOTIFICATION_GROUP_ID, NOTIFICATION_GROUP_NAME);
        notificationManager.createNotificationChannelGroup(mGroup);

        NotificationCompat.Builder builder = new NotificationCompat.Builder
                (context, NOTIFICATION_CHANNEL_ID)

                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_subtext))
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setContentIntent(pIpanel)

                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

        notification = builder.build();
    }

    if (notificationManager != null) {
        notificationManager.notify(NOTIFICATION_ID, notification);
    }