2014-11-11 12 views
5

Đang sử dụng thông báo phân tích cú pháp trong ứng dụng của tôi. Đang nhận cảnh báo thông báo bằng cách sử dụng GcmBroadcastReceiver. Nhưng tôi thấy rất nhiều thông báo nhận được từ ứng dụng của mình. Tôi nghĩ rằng để cập nhật thông báo trong thanh trạng thái vì vậy tôi đã sử dụng máy thu tùy chỉnh. Khi nhận được thông báo thông qua GcmBroadcastReceiver, người phát sóng tùy chỉnh của tôi đã gọi. Vì vậy, trong thanh trạng thái của tôi có cả thông báo gcm và tùy chỉnh. Tôi chỉ muốn thông báo tùy chỉnh. Làm thế nào để giải quyết vấn đề này?Nhận thông báo Phân tích cú pháp bằng cách sử dụng công cụ phát sóng tùy chỉnh

đang Manifest:

<receiver android:name="com.parse.ParseBroadcastReceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.USER_PRESENT" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name="com.parse.GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

      <!-- IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name. --> 
      <category android:name="com.packagename" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name="com.parse.ParsePushBroadcastReceiver" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.parse.push.intent.RECEIVE" /> 
      <action android:name="com.parse.push.intent.DELETE" /> 
      <action android:name="com.parse.push.intent.OPEN" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name="com.packagename.IncomingReceiver" 
     android:enabled="true" 
     android:exported="false" > 
     <intent-filter> 

      <action android:name="com.packagename.UPDATE_STATUS" /> 
     </intent-filter> 
    </receiver> 

lớp Application:

Parse.initialize(this, "app id", "client id"); 

Incomingreceiver lớp:

public class IncomingReceiver extends BroadcastReceiver { 
    private static final int NOTIFICATION_ID = 1; 
    public static int numMessages = 0; 


    @Override 
    public void onReceive(Context context, Intent intent) { 


     try { 
      String action = intent.getAction(); 
      JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 
      if (action.equalsIgnoreCase("com.packagename.UPDATE_STATUS")) { 
       String title = "appname"; 

       if (json.has("header")) 
        title = json.getString("header"); 

       generateNotification(context, title, json,contenttext); 
      } 
     } catch (JSONException e) { 
      Log.d("jsonexc", "JSONException: " + e.getMessage()); 
     } 
    } 

    private void generateNotification(Context context, String title, JSONObject json, String contenttext) { 
     Intent intent = new Intent(context, NewActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); 

     NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.app_icon).setContentTitle(title).setContentText("contenttext").setNumber(++numMessages); 

     mBuilder.setContentIntent(contentIntent); 
     mNotifM.notify(NOTIFICATION_ID, mBuilder.build()); 

    } 
} 
+0

Xin vui lòng gửi mã mà u sử dụng – Siva

+0

@Gayathiri mã được cập nhật? – Erum

+0

bản sao có thể có của [SDK phân tích cú pháp triển khai BroadcastReceiver tùy chỉnh cho thông báo đẩy trong Android] (http://stackoverflow.com/questions/29657757/parse-sdk-implement-custom-broadcastreceiver-for-push-notifications-in-android) – Cristik

Trả lời

11

Thay thế nó

<receiver 
     android:name="com.parse.ParsePushBroadcastReceiver" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.parse.push.intent.RECEIVE" /> 
      <action android:name="com.parse.push.intent.DELETE" /> 
      <action android:name="com.parse.push.intent.OPEN" /> 
     </intent-filter> 
</receiver> 

đến của bạn:

<receiver 
     android:name=".MyReceiver" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="com.parse.push.intent.RECEIVE" /> 
      <action android:name="com.parse.push.intent.DELETE" /> 
      <action android:name="com.parse.push.intent.OPEN" /> 
     </intent-filter> 
    </receiver> 

và tạo lớp:

public class MyReceiver extends ParsePushBroadcastReceiver { 

    protected void onPushReceive(Context mContext, Intent intent) { 
    //enter your custom here generateNotification(); 
    } 

} 
5

Chỉ cần loại bỏ super.onPushReceive (bối cảnh, mục đích); từ bộ thu tùy chỉnh của bạn để nhận được một thông báo tùy chỉnh.

+0

Hoàn hảo. Điều này làm việc cho tôi. – Anirudh

0

Trong file manifest của bạn, bạn đang sử dụng

com.parse.GcmBroadcastReceiver

thay vì sử dụng broadcastreciver tùy chỉnh của bạn, trong bạn là trường hợp thay đổi đơn giản > com.parse.GcmBroadcastReceiver để com.yourpackagename. IncomingReceiver

Không kiểm tra BroadcastReceiver trong trình mô phỏng nó sẽ không hoạt động để kiểm tra nó trong một Android thực sự

Xem repo này trong đó sử dụng một broadcastreciver tùy chỉnh cho thông báo phân tích cú pháp đẩy https://github.com/srini-hashinc/IIYO

+0

Vui lòng thêm liên kết "repo này"! –

+0

https://github.com/srini-hashinc/IIYO –

+0

Là một sidenote, thông báo đẩy DO làm việc trên trình giả lập, tuy nhiên bạn cần cài đặt Dịch vụ của Google Play. – Waclock

1

tôi chỉ cần thêm PushReceiver tùy chỉnh của tôi với tên gói giống như ParsePushReceiver

package com.parse; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
import android.os.Build; 
import android.os.Bundle; 

import com.squareup.otto.EventBus; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.Locale; 
import java.util.Random; 

public class ParsePushReceiver extends ParsePushBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String intentAction = intent.getAction(); 
     byte var5 = -1; 
     switch (intentAction.hashCode()) { 
      case -824874927: 
       if (intentAction.equals("com.parse.push.intent.DELETE")) { 
        var5 = 1; 
       } 
       break; 
      case -269490979: 
       if (intentAction.equals("com.parse.push.intent.RECEIVE")) { 
        var5 = 0; 
       } 
       break; 
      case 374898288: 
       if (intentAction.equals("com.parse.push.intent.OPEN")) { 
        var5 = 2; 
       } 
     } 

     switch (var5) { 
      case 0: 
       onPushReceive(context, intent); 
       break; 
      case 1: 
       super.onPushDismiss(context, intent); 
       break; 
      case 2: 
       super.onPushOpen(context, intent); 
     } 
    } 

    @Override 
    protected void onPushReceive(Context context, Intent intent) { 

     try { 
      JSONObject pushData = new JSONObject(intent.getStringExtra("com.parse.Data")); 

      String action; 
      action = pushData.optString("action", null); 

      if (action != null) { 
       Bundle notification = intent.getExtras(); 
       Intent broadcastIntent = new Intent(); 
       broadcastIntent.putExtras(notification); 
       broadcastIntent.setAction(action); 
       broadcastIntent.setPackage(context.getPackageName()); 
       context.sendBroadcast(broadcastIntent); 
      } 

      if (pushData.has(Push.PUSH_TYPE)) { 
       String type = pushData.getString(Push.PUSH_TYPE); 
       switch (PushType.fromString(type)) { 
        case chat: 
        case booked: 
        case rate: 
        case unknown: 
         break; 
       } 
      } else { 
       sendGlobalNoti(context, intent, null); 
      } 
     } catch (JSONException var7) { 
      PLog.e("ParsePushReceiver", "Unexpected JSONException when receiving push data: ", var7); 
     } 
    } 

    private void sendChattingNoti(Context context, Intent intent, PushType pushType) { 

     JSONObject pushData = this.getPushData(intent); 
     if (((User) ParseUser.getCurrentUser()).getChattingNotiEnabled() 
       && pushData != null 
       && pushData.has("title")) { 
      NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(context); 

      String title = pushData.optString("title", context.getString(R.string.app_name)); 
      String message = pushData.optString("alert", "Notification received."); 

      switch (pushType) { 
       case booked: 
        message = context.getString(R.string.lesson_booked_message); 
        break; 
       case lessonConfirmed: 
        message = context.getString(R.string.lesson_confirmed); 
        break; 
       case lessonCanceled: 
        message = context.getString(R.string.lesson_canceled); 
        break; 
      } 

      builder.setContentTitle(title); 
      builder.setContentText(message); 
      builder.setTicker(title + ": " + message); 
      builder.setAutoCancel(true); 
      builder.setOnlyAlertOnce(false); 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
       builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_notification)); 
      builder.setSmallIcon(R.drawable.ic_notification); 

      Intent chatIntent = new Intent(Keys.IntentKeys.ACTION_SHOW_CHAT); 
      chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
      chatIntent.addCategory(Intent.CATEGORY_DEFAULT); 
      String chatId = pushData.optString(Push.PUSH_CHAT_ID, null); 
      if (chatId != null) 
       chatIntent.putExtra(Keys.EXTRA_CHAT_ID, chatId); 

      PendingIntent pending = PendingIntent.getActivity(context, 0, chatIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      builder.setContentIntent(pending); 

      Notification noti = builder.build(); 
      noti.defaults |= Notification.DEFAULT_VIBRATE; 
      noti.defaults |= Notification.DEFAULT_SOUND; 
      noti.defaults |= Notification.DEFAULT_LIGHTS; 
      nm.notify(pushType.ordinal(), noti); 
     } 
    } 

    private void sendGlobalNoti(Context context, Intent intent, String defaultAlert) { 
     // Global Notification 
     if (((User) ParseUser.getCurrentUser()).getGlobalNotiEnabled()) { 
      Notification notification = getNotification(context, intent, defaultAlert); 
      if (notification != null) 
       ParseNotificationManager.getInstance().showNotification(context, notification); 
     } 
    } 

    protected Notification getNotification(Context context, Intent intent, String defaultAlert) { 
     JSONObject pushData = this.getPushData(intent); 
     if (pushData != null && (pushData.has("alert") || pushData.has("title"))) { 
      String title = pushData.optString("title", ManifestInfo.getDisplayName(context)); 
      String alert = pushData.optString("alert", defaultAlert == null ? "Notification received." : defaultAlert); 
      String tickerText = String.format(Locale.getDefault(), "%s: %s", new Object[]{title, alert}); 
      Bundle extras = intent.getExtras(); 
      Random random = new Random(); 
      int contentIntentRequestCode = random.nextInt(); 
      int deleteIntentRequestCode = random.nextInt(); 
      String packageName = context.getPackageName(); 
      Intent contentIntent = new Intent("com.parse.push.intent.OPEN"); 
      contentIntent.putExtras(extras); 
      contentIntent.setPackage(packageName); 
      Intent deleteIntent = new Intent("com.parse.push.intent.DELETE"); 
      deleteIntent.putExtras(extras); 
      deleteIntent.setPackage(packageName); 
      PendingIntent pContentIntent = PendingIntent.getBroadcast(context, contentIntentRequestCode, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      NotificationCompat.Builder parseBuilder = new NotificationCompat.Builder(context); 
      parseBuilder.setContentTitle(title).setContentText(alert).setTicker(tickerText).setSmallIcon(this.getSmallIconId(context, intent)).setLargeIcon(this.getLargeIcon(context, intent)).setContentIntent(pContentIntent).setDeleteIntent(pDeleteIntent).setAutoCancel(true).setDefaults(-1); 
      if (alert != null && alert.length() > 38) { 
       parseBuilder.setStyle((new NotificationCompat.Builder.BigTextStyle()).bigText(alert)); 
      } 

      return parseBuilder.build(); 
     } else { 
      return null; 
     } 
    } 

    private JSONObject getPushData(Intent intent) { 
     try { 
      return new JSONObject(intent.getStringExtra("com.parse.Data")); 
     } catch (JSONException var3) { 
      PLog.e("ParsePushReceiver", "Unexpected JSONException when receiving push data: ", var3); 
      return null; 
     } 
    } 
} 
Các vấn đề liên quan