극광 전송 사용자 정의 알림 표시줄 알림음

4209 단어 통지란
말 많이 안 하고 바로 코드.
  /**
     *          
     * @param context
     * @param bundle
     */

    private void processCustomMessage(Context context, Bundle bundle) {

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(context);

        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

        notification.setAutoCancel(true)
                .setContentText(extras)
                .setContentTitle(context.getString(R.string.app_name))
                .setSmallIcon(R.mipmap.ic_launcher);
        if (!TextUtils.isEmpty(extras)) {
            try {
                JSONObject extraJson = new JSONObject(extras);
                if (null != extraJson && extraJson.length() > 0) {

                    String sound = extraJson.getString("order_type");
                    String id = extraJson.getString("id");

//                    order_type    1     2     
                    Log.e(TAG,sound+"---------------");
                    //        
                    if("1".equals(sound)){
                        notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.neworder));
                    }else if (sound.equals("2")){
                        notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.cancle));
                    }

        //          
        Intent mIntent = new Intent(context,DetailsAty.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mIntent.putExtra(ExtraKey.ORDER_ID,id);
        mIntent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);

        notification.setContentIntent(pendingIntent);
        //       
        notificationManager.notify(bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID), notification.build());
            }
        } catch (JSONException e) {

        }

        }
    }

2. 극광 전송 알림을 받는 위치에서 처리
else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver]           ");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver]            ID: " + notifactionId);
            //       
            processCustomMessage(context,bundle);

        } 

3. 주의해야 할 사항:
3.1 알림 표시줄 사용자 지정 시
 notification.setAutoCancel(true)
                .setContentText(extras)
                .setContentTitle(context.getString(R.string.app_name))
                .setSmallIcon(R.mipmap.ic_launcher);

이 몇 개의 속성은 반드시 설정해야 한다. 그렇지 않으면 폭발할 것이다
3.2 알림 표시줄을 사용자 정의한 후 기존 알림을 덮어써야 합니다. 그렇지 않으면 하나의 푸시로 두 개의 정보를 표시할 수 있습니다.
방법은 다음과 같습니다.
   //       
        notificationManager.notify(bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID), notification.build());

이 방법의 첫 번째 매개 변수는 원래 전송 알림 id입니다
 
주: 1. 만약에 극광 알림 메시지를 가면 극광 전송 내용이 모두 비어야 한다. 알림 표시줄의 내용은 모두 사용자 정의 notification에 전시된다. 그렇지 않으면 잠금 상태에서 두 개의 전송을 받을 수 있다. (즉 잠금 화면 하향 시스템의 기본 알림음, 잠금 해제 후 사용자 정의 소리를 방송한다)
2、set Sound 방법으로 오디오를 설정하면 "See the documentation of set Sound () for what to use instead with android. media. Audio Attributes to qualify your playback use case"문제가 발생할 수 있으므로 방송할 수 없습니다
나의 해결 방법
 if("1".equals(sound)){
                        if (mediaPlayer1==null)
                            mediaPlayer1 = MediaPlayer.create(context, R.raw.neworder);
                        mediaPlayer1.start();
                    }else if (sound.equals("2")){
                        if (mediaPlayer2==null)
                            mediaPlayer2 = MediaPlayer.create(context, R.raw.cancle);
                        mediaPlayer2.start();
                    }

좋은 웹페이지 즐겨찾기