抱歉我的英文。我创建自定义通知(使用parse.com和lib)。我希望这样:我收到通知 - >点击这个 - >开启新活动。我花了很多次来解决这个问题,但没有成功。我的代码:
public class Receiver extends ParsePushBroadcastReceiver {
NotificationCompat.Builder mBuilder;
int mNotificationId = 001;
@Override
public void onPushOpen(Context context, Intent intent) {
ParseAnalytics.trackAppOpenedInBackground(intent);
Intent i = new Intent(context, Main.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
@Override
public void onReceive(Context context, Intent intent) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setVibrate(new long[]{1000, 1000});
mBuilder.setSmallIcon(R.drawable.icon); //You can change your icon
mBuilder.setContentText("Content");
mBuilder.setContentTitle("Title");
mBuilder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.beep1));
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, mBuilder.build());
}
}
清单
<receiver android:name=".Receiver"
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>
答案 0 :(得分:1)
我可以使用以下代码段打开预期的活动!
@Override
protected void onPushOpen(Context context, Intent intent) {
try {
super.onPushOpen(context, intent);
ParseAnalytics.trackAppOpenedInBackground(intent);
PushService.setDefaultPushCallback(context, NewFragment.class);
ParseAnalytics.trackAppOpenedInBackground(intent);
Intent i = new Intent(context, NewFragment.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} catch (Exception e) {
Log.d(TAG, "onPushOpen Error : " + e);
}
}
答案 1 :(得分:1)
您可以启动一项新活动,该活动可以在sperate屏幕/布局上显示新通知。
为此,请转到ParseApplication.java
public class ParseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, keys.applicationId, keys.clientKey);
PushService.setDefaultPushCallback(this, NewActivity.java);
ParseInstallation.getCurrentInstallation().saveEventually();
}
基本上,当您收到通知并且用户点击时,它会加载NewActivity.java,而NewActivity.java会打开xml文件。
现在NewActivity.java
转到onCreate
setContentView
的{{1}}
setContentView(R.layout.**layout name**);
把你的xml文件。
不要忘记在清单中添加活动。