Android:如何发出通知以使其与API 26+及更低版本兼容?

时间:2018-07-03 13:21:19

标签: java android android-studio

我是android新手。

想发出通知,但不知道我的代码有什么问题。

此外,我想知道“ Notification.builder”和“ NotificationCompat.builder”之间的区别。两者都可以在API 26+及更低版本中使用吗?

MainActivity.java

package com.example.notificationtest2;

public class MainActivity extends AppCompatActivity {
    private String CHANNEL_ID="id_1";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.O){
                    NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_HIGH);
                    notificationChannel.setDescription("This is description of my channel");
                    NotificationManager notificationManager=getSystemService(NotificationManager.class);
                    notificationManager.createNotificationChannel(notificationChannel);

                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("Title")
                            .setContentText("content")
                            .setPriority(NotificationCompat.PRIORITY_HIGH);
                }else{
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("Title")
                            .setContentText("content")
                            .setPriority(NotificationCompat.PRIORITY_HIGH);
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

Bundle bundle = new Bundle();
        bundle.putString("Notification", body);
        intent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.BigTextStyle inboxStyle = new NotificationCompat.BigTextStyle();
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(body)
                .setStyle(inboxStyle.bigText(body))
                .setAutoCancel(true)
                .setSound(defaultSoundUri);

        Notification notification;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                String CHANNEL_ID = "CHANNEL_ID";
                int importance = NotificationManager.IMPORTANCE_HIGH;

                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, "WakeUp", importance);
                notificationBuilder.setChannelId(CHANNEL_ID);
                notificationBuilder.setContentIntent(pendingIntent);
                notificationBuilder.setBadgeIconType(BADGE_ICON_SMALL);    
                notification = notificationBuilder.build();
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.createNotificationChannel(mChannel);
                mNotificationManager.notify(0, notification);
            } else {
                notification = notificationBuilder.build();
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                if (mNotificationManager != null) {
                    mNotificationManager.notify(0, notification);
                }
            }

答案 1 :(得分:0)

可能会帮助您,请尝试以下操作:-

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("app_channel", "Demo Notification", NotificationManager.IMPORTANCE_LOW);
        channel.setSound(null, null);
        NotificationManager mManager = (NotificationManager) reactContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mManager.createNotificationChannel(channel);
}
Intent intent = new Intent(reactContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(reactContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder(reactContext)
        .setContentTitle("Demo " + (isMuted ? "(Muted) - " : "- ") + audioSDK.getAudioSDKState().value)
        .setContentText("Demo Text" + CHANNEL)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentIntent(contentIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        notificationBuilder.setChannelId("app_channel");
return notificationBuilder.build();