notification notification code implementation

一。public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JIGUANG-Example";


@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));


if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...


} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] Received the push custom message: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);


} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent. getAction())) {
Log.d(TAG, "[MyReceiver] received a push notification");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[MyReceiver] received a push notification"); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); The ID of the down notification: " + notifactionId);


} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] user clicked to open the notification");


//Open Custom Activity
Intent i = new Intent(context, MainActivity.class);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);


} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] Go to RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//Here, process the code according to the content of JPushInterface.EXTRA_EXTRA, such as opening a new Activity, opening a web page, etc..


} else if(JPushInterface.ACTION_CONNECTION_CHANGE .equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+ connected);
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
} catch (Exception e){


}


}


// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
Log.i(TAG, "This message has no Extra data");
continue;
}


try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it =  json.keys();


while (it.hasNext()) {
String myKey = it.next();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " +json.optString(myKey) + "]");
}
} catch (JSONException e) {
Log.e(TAG, "Get message extra JSON error!");
}


} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
} //send msg to MainActivity private void processCustomMessage(Context context, Bundle bundle) { String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);






String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
Intent intent = new Intent(MainActivity.ACTION);
intent.putExtra(MainActivity.MESSAGE,message);
intent.putExtra(MainActivity.EXTRAS,extra);
context.sendBroadcast(intent);
}
}








二。public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;
    private Button mBtn4;
    private Button mBtn5;
    private Button mBtn6;


    public static final String ACTION = "aaa";
    public static final String TITLE = "title";
    public static final String MESSAGE = "message";
    public static final String EXTRAS = "extras";
    private MyBroad myBroad;


    NotificationManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();


        //注册广播
        myBroad = new MyBroad();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION);
        registerReceiver(myBroad,filter);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroad);
    }


    public class MyBroad extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra(MESSAGE);
            String extra = intent.getStringExtra(EXTRAS);
            Bean bean = new Gson().fromJson(message, Bean.class);
            if(bean.getCode()==100){
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
            }else if(bean.getCode()==200){
                showNotification(bean.getMsg());
            }


        }
    }


    private void initView() {
        mBtn1 = (Button) findViewById(R.id.mBtn1);
        mBtn2 = (Button) findViewById(R.id.mBtn2);
        mBtn3 = (Button) findViewById(R.id.mBtn3);
        mBtn4 = (Button) findViewById(R.id.mBtn4);
        mBtn5 = (Button) findViewById(R.id.mBtn5);
        mBtn6 = (Button) findViewById(R.id.mBtn6);


        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
        mBtn3.setOnClickListener(this);
        mBtn4.setOnClickListener(this);
        mBtn5.setOnClickListener(this);
        mBtn6.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mBtn1:
                showNotification("发起通知");
            break;
            case R.id.mBtn2:
                showNotification2("打开一个Activity");
                break;
            case R.id.mBtn3:
                showNotification3("打开一组Activity");
                break;
            case R.id.mBtn4:
                manager.cancel(1);
                break;
            case R.id.mBtn5:
                manager.cancel("b",2);
                break;
            case R.id.mBtn6:
                manager.cancelAll();
                break;
        }
    }


    private void showNotification(String msg){
        //1. Get NotificationManager object
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //2. Get Notification object Set icon title content
        Notification notification = new NotificationCompat.Builder(MainActivity.this, "a")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My message")
                .setContentText("Received a message, please check it")
                .build();
        //3. Initiate notification
        manager.notify( 1,notification);
    }
    private void showNotification2(String msg){
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this,1,
                intent,PendingIntent.FLAG_UPDATE_CURRENT);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(MainActivity.this, "a")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我的消息")
                .setContentText("收到一条消息,请查收")
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
        manager.notify("b",1,notification);
    }
    private void showNotification3(String msg){
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        Intent intent1 = new Intent(MainActivity.this, Main3Activity.class);
        Intent[] intents={intent,intent1};
        PendingIntent pendingIntent = PendingIntent.getActivities(MainActivity.this, 200, intents, PendingIntent.FLAG_UPDATE_CURRENT);
        manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(MainActivity.this, "123")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我的消息")
                .setContentText("收到一条消息,请查收")
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(1,notification);
    }
}






三。public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }
}




四。public class Bean {
    private String msg;
    private int code;


    public Bean() {
    }


    public Bean(String msg, int code) {
        this.msg = msg;
        this.code = code;
    }


    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }


    public int getCode() {
        return code;
    }


    public void setCode(int code) {
        this.code = code;
    }


    @Override
    public String toString() {
        return "Bean{" +
                "msg='" + msg + '\'' +
                ", code=" + code +
                '}';
    }
}




五。main2Acitivity  main3Activity  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324736561&siteId=291194637