step1: MainActivity.java 主页通知事件
package com.example.user.mathgame;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String EXTRA_IS_LEAVING = "EXTRA_IS_LEAVING";
private boolean isLeaving = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_splash = findViewById(R.id.btn_splash);
Button btn_broadcast = findViewById(R.id.btn_broadcast);
btn_splash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationHelper notificationHelper = new NotificationHelper(MainActivity.this);
NotificationManager mManager = notificationHelper.getManager();
notificationHelper.sendNotification(1,mManager);
}
});
btn_broadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TextActivity.class);
startActivity(intent);
}
});
}
public static Intent getCallingIntent(Context context, boolean isLeaving) {
Intent intent = new Intent(context, TextActivity.class);
intent.putExtra(EXTRA_IS_LEAVING, isLeaving);
return intent;
}
}
step2: NotificationHelper.java 通知类
package com.example.user.mathgame;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
public class NotificationHelper extends ContextWrapper {
public static final String ChannelID = "ChannelID";
public static final String Channel1ID = "Channel1ID";
public static final String Channel2ID = "Channel2ID";
public static final String Channel1Name = "Channel1Name";
public static final String Channel2Name = "Channel2Name";
private Context mContext;
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
this.mContext = base;
createChannels();
}
public void createChannels() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationChannel channel1 = new NotificationChannel(Channel1ID, Channel1Name, NotificationManager.IMPORTANCE_DEFAULT);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel1);
NotificationChannel channel2 = new NotificationChannel(Channel2ID, Channel2Name, NotificationManager.IMPORTANCE_DEFAULT);
channel2.enableLights(true);
channel2.enableVibration(true);
channel2.setLightColor(R.color.colorPrimary);
channel2.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel2);
}
public NotificationManager getManager() {
if (mManager == null)
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
return mManager;
}
public void sendNotification(int id,NotificationManager mManager) {
Intent intent = MainActivity.getCallingIntent(mContext, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_ONE_SHOT);
mManager.notify(id, getChannel1NotificationBuilder("张飞", "关羽", pendingIntent).build());
}
private PendingIntent createLeavingIntent(int requestCode) {
Intent intent = null;//MainActivity.getCallingIntent(context, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return PendingIntent.getActivity(mContext, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
}
public NotificationCompat.Builder getChannelNotificationBuilder() {
return new NotificationCompat.Builder(getApplicationContext(), ChannelID);
}
public NotificationCompat.Builder getChannel1NotificationBuilder(String title, String message, PendingIntent pendingIntent) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
return new NotificationCompat.Builder(getApplicationContext(), Channel1ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}
public NotificationCompat.Builder getChannel2NotificationBuilder(String title, String message) {
return new NotificationCompat.Builder(getApplicationContext(), Channel2ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher);
}
}
step3: D:\project\push\notification\four\v7\java\MathGame\app\src\main\AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.mathgame">
<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>
<activity android:name=".TextActivity"/>
</application>
</manifest>
end