44import android .content .Context ;
55import android .content .Intent ;
66import android .os .Bundle ;
7+ import android .os .Build ;
8+ import android .graphics .Color ;
9+ import android .app .Notification ;
10+ import android .app .NotificationManager ;
11+ import android .app .NotificationChannel ;
12+ import android .app .PendingIntent ;
13+ import java .lang .System ;
14+
15+ import android .R ;
16+
17+ import android .support .v4 .app .NotificationCompat ;
718
819import com .facebook .react .bridge .Arguments ;
920import com .facebook .react .bridge .WritableMap ;
1021
1122import cn .jpush .android .api .JPushInterface ;
12- import me .youchai .rnpush .Notification ;
1323import me .youchai .rnpush .RNPushModule ;
1424import me .youchai .rnpush .utils .Logger ;
1525
@@ -31,9 +41,8 @@ public void onReceive(Context context, Intent data) {
3141 String title = bundle .getString (JPushInterface .EXTRA_TITLE );
3242 String content = bundle .getString (JPushInterface .EXTRA_MESSAGE );
3343 String extras = bundle .getString (JPushInterface .EXTRA_EXTRA );
34- Logger .i ("receive message " + title + " " + content );
3544
36- RNPushModule .onNotification (new Notification (
45+ RNPushModule .onNotification (new me . youchai . rnpush . Notification (
3746 id , title , content , extras
3847 ));
3948 } else if (JPushInterface .ACTION_NOTIFICATION_RECEIVED .equals (data .getAction ())) {
@@ -42,19 +51,65 @@ public void onReceive(Context context, Intent data) {
4251 String title = bundle .getString (JPushInterface .EXTRA_NOTIFICATION_TITLE );
4352 String content = bundle .getString (JPushInterface .EXTRA_ALERT );
4453 String extras = bundle .getString (JPushInterface .EXTRA_EXTRA );
45- Logger .i ("receive notification " + title + " " + content );
4654
47- RNPushModule .onNotification (new Notification (
55+ if (Build .VERSION .SDK_INT >= 26 ) {
56+ Logger .i ("android 8.0 以上推送广播处理" );
57+ NotificationManager notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
58+
59+ int notificationId = bundle .getInt (JPushInterface .EXTRA_NOTIFICATION_ID ); // 定义通知id
60+ // 创建 channel
61+ String channelId = context .getPackageName ();// 通知渠道id
62+ String channelName = "viewers" ; // "PUSH_NOTIFY_NAME"; //通知渠道名
63+ int importance = NotificationManager .IMPORTANCE_HIGH ; // 通知级别
64+ NotificationChannel channel = new NotificationChannel (channelId , channelName , importance );
65+ channel .setLightColor (Color .RED );
66+ channel .setShowBadge (true );
67+ channel .setVibrationPattern (new long [] { 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 });
68+ notificationManager .createNotificationChannel (channel );
69+
70+ // 通知点击操作的参数
71+ Bundle intentParams = new Bundle ();
72+ intentParams .putInt (JPushInterface .EXTRA_NOTIFICATION_ID , bundle .getInt (JPushInterface .EXTRA_NOTIFICATION_ID , 0 ));
73+ intentParams .putString (JPushInterface .EXTRA_TITLE , title );
74+ intentParams .putString (JPushInterface .EXTRA_MESSAGE , content );
75+ intentParams .putString (JPushInterface .EXTRA_EXTRA , extras );
76+ // 用于响应通知点击操作的 intent
77+ Intent intent0 = new Intent (context , JPushReceiver .class );
78+ intent0 .setAction (JPushInterface .ACTION_NOTIFICATION_OPENED );
79+ intent0 .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TOP );
80+ intent0 .putExtras (intentParams );
81+ PendingIntent pi = PendingIntent .getBroadcast (context , notificationId , intent0 , PendingIntent .FLAG_UPDATE_CURRENT );
82+
83+ // 获取状态栏显示的通知图标
84+ int drawableId = context .getResources ().getIdentifier ("ic_notification" , "drawable" , context .getPackageName ());
85+ // 创建通知
86+ NotificationCompat .Builder builder = new NotificationCompat .Builder (context , channelId );
87+ builder .setContentTitle (title ) // 设置通知栏标题
88+ .setContentText (content )
89+ .setWhen (System .currentTimeMillis ()) // 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
90+ .setSmallIcon (drawableId )
91+ .setChannelId (channelId )
92+ .setPriority (NotificationCompat .PRIORITY_DEFAULT )
93+ .setAutoCancel (true )
94+ .setContentIntent (pi );
95+ Notification notification = builder .build ();
96+ notification .flags |= Notification .FLAG_AUTO_CANCEL ;
97+
98+ if (notificationManager != null ) {
99+ notificationManager .notify (notificationId , notification );
100+ }
101+ } else {
102+ RNPushModule .onNotification (new me .youchai .rnpush .Notification (
48103 id , title , content , extras
49- ));
104+ ));
105+ }
50106 } else if (JPushInterface .ACTION_NOTIFICATION_OPENED .equals (data .getAction ())) {
51107 // 这里点击通知跳转到指定的界面可以定制化一下
52108 try {
53109 String id = String .valueOf (bundle .getInt (JPushInterface .EXTRA_NOTIFICATION_ID , 0 ));
54110 String title = bundle .getString (JPushInterface .EXTRA_NOTIFICATION_TITLE );
55111 String content = bundle .getString (JPushInterface .EXTRA_ALERT );
56112 String extras = bundle .getString (JPushInterface .EXTRA_EXTRA );
57- Logger .d ("notification click " + title );
58113
59114 Intent intent = new Intent ();
60115 JSONObject jExtra = new JSONObject (extras );
@@ -71,7 +126,7 @@ public void onReceive(Context context, Intent data) {
71126 context .startActivity (intent );
72127 }
73128
74- RNPushModule .onNotificationClick (new Notification (
129+ RNPushModule .onNotificationClick (new me . youchai . rnpush . Notification (
75130 id , title , content , extras
76131 ));
77132 } catch (Exception e ) {
0 commit comments