2015-04-08 37 views
0

我需要帮助实现一个ParsePushBroadcastReceiver, 为了使用它来解析一个JSON对象,选择一个活动开始。Parse.com android通知 - 获取数据到BrodcastReciever

谢谢。

我试着

<receiver 
      android:name="com......BroadcastReceiver" 
      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....BroadcastReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
      </intent-filter> 
     </receiver> 

但没有成功......

我的应用程序类

public class AppApplication extends Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 

     System.out.println("vrum"); 
     Parse.initialize(this, "...", ".."); 
     PushService.setDefaultPushCallback(this, HomeActivity.class); 
     ParseInstallation.getCurrentInstallation().saveInBackground(); 

    } 

当我点击通知我进入HomeActivity ... s AME的事情,如果我删除了这一行

PushService.setDefaultPushCallback(this, HomeActivity.class); 
+0

阅读此https://parse.com/tutorials/android-push-notifications。你可以有一个自定义类扩展'ParseBroadcastReceiver' – Raghunandan

+0

http://pastebin.com/Djsk4HCk这是我的代码。你能告诉我我错了吗?当我单击通知时,我打开HomeActivity ...不调用CustomParseBroadcastReceiver方法onReceive .. –

+0

这不是必需的。 'PushService.setDefaultPushCallback(this,HomeActivity.class);' – Raghunandan

回答

1
public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver { 

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

    // Sample code 
       JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 
       final String notificationTitle = json.getString("title").toString(); 
       final String notificationContent = json.getString("alert").toString(); 
       final String uri = json.getString("uri"); 

    //Create a taskstack builder - this is just sample snippet to give an idea 
       Intent resultIntent = null; 
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
resultIntent = new Intent(context, NotificationOpenActivity.class); 
stackBuilder.addParentStack(NotificationOpenActivity.class); 
stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = 
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 


       // Customize your notification 
       NotificationCompat.Builder builder = 
         new NotificationCompat.Builder(context) 
           .setSmallIcon(R.mipmap.ic_notification_icon) 
           .setContentTitle(notificationTitle) 
           .setContentText(notificationContent) 
           .setGroup(GROUP_SHORTR_NOTIFS) 
           .setContentIntent(resultPendingIntent) 
           .setAutoCancel(true) 
           .setVisibility(Notification.VISIBILITY_PUBLIC) 
           .setDefaults(Notification.DEFAULT_VIBRATE) 
           .setStyle(new NotificationCompat.BigTextStyle() 
             .bigText(notificationContent)); 

       int mNotificationId = 001; 
       NotificationManager mNotifyMgr = 
         (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
       mNotifyMgr.notify(mNotificationId, builder.build()); 


      } catch (JSONException e) { 
       Log.d(TAG, e.getMessage()); 
      } 

     } 
    } 

添加下面的清单。

<receiver 
      android:name=".receivers.ParseCustomBroadcastReceiver" 
      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> 

基本上,对于从您的问题中提取的清单编辑,只需编辑android:name属性即可。

希望这会有所帮助!