解析onPushOpen从未调用过

时间:2015-01-06 19:19:02

标签: android parse-platform google-cloud-messaging

我已经使用parse实现了推送。将参数添加到清单文件中:

    <receiver
        android:name="com.emaborsa.cablePark.parse.GcmBroadcastReceiver"
        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>

Java代码:

public void onReceive(Context context, Intent intent) {
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String text = context.getResources().getString(R.string.msg_newDataEp);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_CLEAR_TOP);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setContentText(text)
            .setContentIntent(contentIntent)
            .setContentTitle(context.getResources().getString(R.string.cableparks));

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(500);

    setResultCode(Activity.RESULT_OK);
}


@Override
protected void onPushOpen(Context context, Intent intent) {
    ParseAnalytics.trackAppOpenedInBackground(intent);
}

我接受了所有测试手机的推送,但是通过点击通知,永远不会调用onPushOpened方法...... 我需要它用于Parse-Analytics。 自调用onPushReceive方法以来,将触发操作com.parse.push.intent.RECEIVE。似乎永远不会解雇com.parse.push.intent.DELETEcom.parse.push.intent.OPEN,因为从不调用这些方法...... 提示?

5 个答案:

答案 0 :(得分:1)

终于找到了解决方案!

在getNotification()中添加此项

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, 0x8000000);
PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode, deleteIntent, 0x8000000);


Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
		NotificationCompat.Builder mBuilder =
		        new NotificationCompat.Builder(context)
		        .setSmallIcon(R.drawable.ic_launcher)
		        .setLargeIcon(notificationLargeIconBitmap)
		        .setContentIntent(pContentIntent).setDeleteIntent(pDeleteIntent)
		        .setContentTitle(title)
		        .setGroup("999")
		        .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +id))
		        .setGroupSummary (true)
		        .setContentText(text)
		      //  .setDefaults(Notification.DEFAULT_ALL)
		        .setNumber(++numMessages);

基本上我们需要在您的notificationbuilder上添加两个操作。

答案 1 :(得分:1)

以下是解决方案:使用来自onPushReceive的Intent过滤器

启动 onPushOpen
@Override
protected void onPushReceive(Context context, Intent intent) 
    {
      Intent i=new Intent("com.parse.push.intent.OPEN");

      PendingIntent resultPendingIntent=PendingIntent.getBroadcast(context,
            0, i, PendingIntent.FLAG_CANCEL_CURRENT);

      int mNotificationId = 001;
      int icon = R.drawable.ic_launcher;



      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            context);
      Notification mNotification = mBuilder
            .setSmallIcon(icon)
            .setTicker("")
            .setWhen(0)
            .setAutoCancel(true)
            .setContentTitle("Heading")
            .setStyle(
                    new NotificationCompat.BigTextStyle()
                            .bigText("Sample Notification"))
            .setSound(RingtoneManager                 .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setLargeIcon(
                    BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.ic_launcher))
            .setContentText(notification)
            .setContentIntent(resultPendingIntent).build();

      NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.notify(mNotificationId, mNotification);

    }

    @Override
    protected void onPushOpen(Context context, Intent intent)
   {
       Log.d("TAG","Inside Push Open");
   }

答案 2 :(得分:0)

这是我的工作应用程序的样子(并且肯定会调用onPushOpen) -

public class MyPushReceiver extends ParsePushBroadcastReceiver {
 @Override
 public void onPushOpen(Context context, Intent intent) {
    // Set a breakpoint or Log.
 }
}

在我的清单中 -

<receiver
        android:name="com.xxx.yyy.MyPushReceiver"
        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>

答案 3 :(得分:0)

@Override
public void onPushReceive(Context context, Intent intent) {
    // your code
    super.onPushReceive(context, intent);
}

super.onPushReceive(context, intent);

中致电onPushReceive

答案 4 :(得分:0)

我发现其他答案部分有效。将pushOpen添加到挂起的intent会调用onPushOpen。但是,您需要在onPushOpen中启动所需的活动。

public void onPushReceive(Context context, Intent intent) {

    // This is similar to an answer here...
    super.onPushReceive(context, intent);
    Bundle extras = i.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");
    // You can pass some values to onPushOpen using extras
    contentIntent.putExtra("SOMEVALUEHERE", true);
    contentIntent.setPackage(packageName);
    Intent deleteIntent = new Intent("com.parse.push.intent.DELETE");
    deleteIntent.setPackage(packageName);
    PendingIntent pContentIntent = PendingIntent.getBroadcast(context, contentIntentRequestCode, contentIntent, 0x8000000);
    PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode, deleteIntent, 0x8000000);

    int iconToUse = R.drawable.YOURDRAWABLE;
    // Android M uses different style for icons BTW
    if (isAndroidM()) iconToUse = R.drawable.notification_icon;

    Notification notif = new NotificationCompat.Builder(context)
            .setContentTitle("YOURTITLE")
            .setContentText("YOURALERT")
            .setSmallIcon(iconToUse)
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pContentIntent).setDeleteIntent(pDeleteIntent)
            .build();

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, notif);
}

public void onPushOpen(Context context, Intent intent) {
    // I got a nullPointer on super, so I commented it for now
    //super.onPushOpen(context,intent);
    Log.d("TAG", "onPushOpen");
    ParseAnalytics.trackAppOpenedInBackground(intent);

    // Here is where you can open some activity based on your notifications
    if (intent.getBooleanExtra("SOMEVALUEHERE", false)) {
        Intent i = new Intent(context, YOURFIRSTCLASS.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    } else {
        Intent i = new Intent(context, YOURCLASS2.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    } 
}