安卓通知栏,亲测可用,带点击监听

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值