Android---通知notification之捕获点击

之前写了一篇关于notification通知的文章
链接:https://www.jianshu.com/p/ba8e6a4f023c

如果要捕获通知的点击,要怎么办。监听通知点击事件,没有对应的API。

做法就是通过广播。
上一篇文章是通过PendingIntent.getActivity来创建PendingIntent,配置intent可以直接跳转activity。现在通过PendingIntent.getBroadcast来创建PendingIntent。当点击了这个通知,会发送一个广播,我们可以通过静态注册对应的广播接受者来接受,然后自己处理逻辑。

步骤1:写广播类以及静态注册
public class NotificationBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        
        UMUtils.getInstace()._frequency(context, "id_121");
        Bundle bundle = intent.getExtras();
        Class activity = (Class) bundle.getSerializable("class");

        Intent mIntent = new Intent(context, activity);
        mIntent.putExtras(bundle);
        mIntent.setFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
        );
        context.startActivity(mIntent);
    }
}

在AndroidManifest注册

 
    
步骤2:编写通知

普通通知:

/**
     * 显示普通通知
     *
     * @param mActivity
     * @param mBundle
     */
    private void showCommonNotification(Class mActivity, Bundle mBundle, String title, String content) {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.mipmap.app_icon);

        builder.setContentTitle(title);
        builder.setContentText(Html.fromHtml(content));

        builder.setAutoCancel(true).setDefaults(
                NotificationCompat.DEFAULT_VIBRATE
                        | NotificationCompat.DEFAULT_SOUND
        );

        Intent intent = new Intent(context, NotificationBroadcastReceiver.class);
        mBundle.putSerializable("class", mActivity);
        intent.putExtras(mBundle);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(pendingIntent);
        Notification n = builder.build();
        NotificationManagerCompat managerCompat =
                NotificationManagerCompat.from(context);
        managerCompat.notify((int) System.currentTimeMillis(), n);
    }

横幅通知:


    /**
     * 显示横幅通知
     *
     * @param mActivity
     * @param mBundle
     * @param
     */
    private void showHangup(Class mActivity, Bundle mBundle, String title, String content) {

        NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setContentTitle(title);
        builder.setContentText(Html.fromHtml(content));

        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setSmallIcon(R.mipmap.ielts_bro_app_icon);

        mBundle.putSerializable("class", mActivity);

        Intent intent = new Intent(context, NotificationBroadcastReceiver.class);
        intent.putExtras(mBundle);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(pendingIntent);
        builder.setFullScreenIntent(pendingIntent, true);
        builder.setAutoCancel(true);
        Notification notification = builder.build();
        manger.notify((int) System.currentTimeMillis(), notification);
    }

你可能感兴趣的:(Android---通知notification之捕获点击)