我有一些Parse代码接收推送数据并设置一个正常的通知,但问题是onPushOpen声明。我想在我的应用程序中打开一个特定的活动,但onPushOpen似乎永远不会被调用或只是不工作?
这是我的代码:
public class NotificationReceiver extends ParsePushBroadcastReceiver {
private static final String TAG = "MYTAG";
@Override
public void onReceive(Context context, Intent intent) {
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
Log.i(TAG, "Notification received");
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data") : "";
JSONObject jObject;
String alert = null;
String title = null;
try {
jObject = new JSONObject(message);
alert = (jObject.getString("alert"));
title = (jObject.getString("title"));
}
catch (JSONException e) {
e.printStackTrace();
}
Log.i(TAG,"alert is " + alert);
Log.i(TAG,"title is " + title);
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(alert)
.setContentText(title);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification.build());
Log.i(TAG, "Notification created");
}
@Override
protected void onPushOpen(Context context, Intent intent) {
Log.i(TAG, "Notification clicked");
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
我似乎无法弄清楚为什么onPushOpen不会启动我请求的活动(MainActivity)。任何帮助将不胜感激。
答案 0 :(得分:1)
我面临着与onPushOpen()
方法相同的问题,但如果您只是想打开自己的活动,那么可以解决一些问题。您可以使用Pending Intent这样解决问题:
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(alert)
.setContentText(title);
// create intent to start your activity
Intent activityIntent = new Intent(context, MainActivity.class);
// create pending intent and add activity intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
// Add as notification
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification.build());
所以没有必要调用onPushOpen()
方法,因为一旦点击通知,待处理的意图就会开始你的活动。