天天看點

極光推送(二)接收通知

步驟(分為兩步)

極光推送(二)接收通知

一.設定通知欄樣式(如果不設定則預設使用自帶的),這裡官方給了三種方式:

1.基礎(定制聲音、震動、閃燈等 Notification 樣式):

BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(MainActivity.this);
builder.statusBarDrawable = R.drawable.jpush_notification_icon;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
    | Notification.FLAG_SHOW_LIGHTS;  //設定為自動消失和呼吸燈閃爍
builder.notificationDefaults = Notification.DEFAULT_SOUND
    | Notification.DEFAULT_VIBRATE
    | Notification.DEFAULT_LIGHTS;  // 設定為鈴聲、震動、呼吸燈閃爍都要
//注意這裡的 1 ,以後會有用
JPushInterface.setPushNotificationBuilder(1, builder);
           

2.定制帶按鈕的Notification樣式(3.0.0及以上版本sdk才支援該樣式,且該樣式在某些與Android原生系統有差異的機型上無法正常顯示):

MultiActionsNotificationBuilder builder = new MultiActionsNotificationBuilder(PushSetActivity.this);
//添加按鈕,參數(按鈕圖檔、按鈕文字、擴充資料)
builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "first", "my_extra1");
builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "second", "my_extra2");
builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "third", "my_extra3");
//注意這裡設定的2,以後會有用
JPushInterface.setPushNotificationBuilder(2, builder);
           

3.進階自定義:

CustomPushNotificationBuilder builder = new
            CustomPushNotificationBuilder(this,
            R.layout.customer_notitfication_layout,
            R.id.icon,
            R.id.title,
            R.id.text);
    // 指定定制的 Notification Layout
    builder.statusBarDrawable = R.mipmap.ic_launcher;
    // 指定最頂層狀态欄小圖示
    builder.layoutIconDrawable = R.mipmap.flag;
    // 指定下拉狀态欄時顯示的通知圖示,同時也注意這裡的3,因為能否顯示自定義的這些通知樣式全靠這些标記
    JPushInterface.setPushNotificationBuilder(3, builder);
           

二.如何顯示你所設定的通知:

通過極光推送的背景發送通知你會發現你所設定的樣式不起作用,那麼如何讓它起作用呢?這裡有兩種方式可以實作:

1.通過配置背景資料:

1).在極光推送背景發送通知時點選可選設定按鈕
極光推送(二)接收通知
2).在通知欄樣式編号那裡填寫上面我所強調需要注意的标記,即可顯示你設定的通知欄樣式
極光推送(二)接收通知

2.通過代碼修改預設通知欄樣式:

CustomPushNotificationBuilder builder = new
            CustomPushNotificationBuilder(this,
            R.layout.customer_notitfication_layout,
            R.id.icon,
            R.id.title,
            R.id.text);
    // 指定定制的 Notification Layout
    builder.statusBarDrawable = R.mipmap.ic_launcher;
    // 指定最頂層狀态欄小圖示
    builder.layoutIconDrawable = R.mipmap.flag;
    // 指定下拉狀态欄時顯示的通知圖示
    JPushInterface.setPushNotificationBuilder(1, builder);
    JPushInterface.setDefaultPushNotificationBuilder(builder);
           

三.擷取通知的資訊

1.編寫接收消息的Receiver

public class JiguangReceiver extends BroadcastReceiver {
    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            openNotification(context,bundle);
        }
    }
    //    點選通知欄資訊啟動Activity
    private void openNotification(Context context, Bundle bundle) {
        Intent mIntent = new Intent(context, Test1Activity.class);
        mIntent.putExtras(bundle);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }
}
           

2.在AndroidManifest.xml檔案中注冊Receiver

<receiver
        android:name=".JiguangReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
            <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
            <action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
            <action android:name="cn.jpush.android.intent.CONNECTION" />
            <category android:name="com.itfitness.jiguang" />
        </intent-filter>
    </receiver>
           

3.在點選通知打開的Activity中展示資訊

public class Test1Activity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        textView = findViewById(R.id.tv);
        Bundle extras = getIntent().getExtras();
        textView.setText(extras.getString(JPushInterface.EXTRA_EXTRA));
    }
}
           

四.效果展示

1.添加字段

極光推送(二)接收通知
極光推送(二)接收通知

image

個人技術部落格: https://myml666.github.io