Parse onPushOpen never called
I have implemented rumors using parsing. added parameters to the manifest file:
<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 code:
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);
}
I get pushes on all of my test phones, but when the notification is clicked, the onPushOpened method is never called ... I need this for Parse-Analytics. The action com.parse.push.intent.RECEIVE
starts from the moment the onPushReceive method is called. com.parse.push.intent.DELETE
and com.parse.push.intent.OPEN
never seem to run as methods are never called ... Hints?
source to share
Finally found a solution!
Add this to your 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);
Basically, we need to add both actions to your notification developer.
source to share
Here's the solution: Start onPushOpen with Intent Filter from onPushReceive
@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");
}
source to share
This is how my working application looks like (and onPushOpen is definitely called) -
public class MyPushReceiver extends ParsePushBroadcastReceiver {
@Override
public void onPushOpen(Context context, Intent intent) {
// Set a breakpoint or Log.
}
}
And in my manifesto -
<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>
source to share
I found the other answers partially worked. Adding pushOpen to the pending intent called onPushOpen. However, you need to start the action you want in 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);
}
}
source to share