原生Notification

public class MainActivity extends AppCompatActivity {

    private NotificationManager m_NotificationManager;
    private PendingIntent m_PendingIntent;
    private int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                showNotification();
            }
        });

        findViewById(R.id.cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                m_NotificationManager.cancelAll();
            }
        });
    }

    /**
     * 设置
     */
    private void showNotification() {
        //根据服务器传过来的数据不同类型就开启不同的activity
        Intent intent = new Intent(this, SecondActivity.class);
        intent.addCategory(WINDOW_SERVICE);
        m_PendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //如果转移内容则用m_Intent();
        Notification notification = new Notification.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("这是测试")
                .setContentText("这是测试的数据内容adhiosaslkadl;sajdlsajdlskadlsandlksadsadsada")
                .setContentIntent(m_PendingIntent)
                .setSmallIcon(R.mipmap.back_arrow)
                .setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_SOUND)//声音
                .setTicker("测试通知来啦")
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(false)
                .build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
//        Notification.FLAG_SHOW_LIGHTS             //三色灯提醒,在使用三色灯提醒时候必须加该标志符
//        Notification.FLAG_ONGOING_EVENT           //发起正在运行事件(活动中)                         等价于setOngoing(true)
//        Notification.FLAG_INSISTENT               //让声音、振动无限循环,直到用户响应 (取消或者打开)
//        Notification.FLAG_ONLY_ALERT_ONCE         //发起Notification后,铃声和震动均只执行一次
//        Notification.FLAG_AUTO_CANCEL             //用户单击通知后自动消失                             等价于setAutoCancel(true)
//        Notification.FLAG_NO_CLEAR                //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
//        Notification.FLAG_FOREGROUND_SERVICE      //表示正在运行的服务

        notification.vibrate = new long[]{0, 300, 500, 700};
        m_NotificationManager.notify(i, notification);//id 不一样才可以提示多条
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值