Android 8*汾ʹNotification֪ͨ޷ʾ

ڣ2021-10-16 05:01:41

Android 8.0 ֪ͨԱΪ֪ͨΪṩɺ͸ͳһķʽAndroid8.0ҲAPI26ʼҪ֪ͨChannel,ʾMainActivityʹû򸲸ѹʱ API


ٷAPIĵ
https://developer.android.com/reference/android/app/NotificationChannel



ֱΪID,,Ҫ


NotificationChannel(String id,CharSequence name, int importance)


Ҫ:


IMPORTANCE_NONE ر֪ͨIMPORTANCE_MIN ֪ͨᵯûʾ״̬ʾIMPORTANCE_LOW ֪ͨᵯʾ״̬ʾIMPORTANCE_DEFAULT ֪ͨᵯʾ״̬ʾIMPORTANCE_HIGH ֪ͨᵯʾ״̬ʾ
ʹδʹNotificationChannel֪ͨĴڰ׿8*汾޷״̬е֪ͨ

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ȡ֪ͨڷ֪ͨ
final NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder notification = new Notification.Builder(this); // һNotification
// ô򿪸֪֪ͨͨԶʧ
notification.setAutoCancel(true);
// ֪ͨͼ
notification.setSmallIcon(R.drawable.packet);
// ֪ͨݵı
notification.setContentTitle("");
// ֪ͨ
notification.setContentText("鿴飡");
//ʹϵͳĬϵĬ
notification.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
//÷ʱ
notification.setWhen(System.currentTimeMillis());
// һActivityIntent
Intent intent = new Intent(MainActivity.this
, DetailActivity.class);
PendingIntent pi = PendingIntent.getActivity(
MainActivity.this, 0, intent, 0);
//֪ͨת
notification.setContentIntent(pi);
//֪ͨ
notificationManager.notify(1, notification.build());

}
}

Ϊ´

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class MainActivity extends Activity {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String myChannelId = "myChannel_01";
String myChannelName = "myChannel";
Intent intent=new Intent(MainActivity.this,DetailActivity.class);
PendingIntent pi=PendingIntent.getActivity(MainActivity.this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
NotificationChannel channel = new NotificationChannel(myChannelId, myChannelName,
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(this, myChannelId)
.setContentTitle("")
.setContentText("鿴")
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setShowWhen(true)
.setSmallIcon(R.drawable.packet)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
.setContentIntent(pi)
.build();
notificationManager.notify(1, notification); //1Ϊ֪ͨID
}
}

ͼʾ֪ͨתDetailActivity

δ룬бд


Ƽ