我正在开发一个Android应用程序,在该应用程序中我使用GCM添加通知服务。问题是我想在单击通知时打开特定 Activity 。我不知道如何执行此任务。我还想更改服务器端和客户端上的更改。 Activity 名称由服务器提供,然后当我单击通知时 Activity 将打开。请帮助我。
我已经尝试过这段代码。
protected void onMessage(Context context, Intent intent) {
Log.i(TAG,
"Received message. Extras:*************************************************** "
+ intent.getExtras());
// String message = getString(R.string.gcm_message);
// displayMessage(context, message);
// notifies user
String inAction = intent.getAction();
ArrayList<Notify> notifyData = new ArrayList<Notify>();
if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {
String json_info = intent.getExtras().getString("data");
Notify notify = new Notify();
if (json_info != null) {
try {
JSONObject jsonObj = new JSONObject(json_info);
notify.setdetail(jsonObj.get("Detail").toString());
notify.setappUpdate(jsonObj.get("App Update").toString());
notify.setcontentUpdate(jsonObj.get("Content Update")
.toString());
notify.setSpecial(jsonObj.get("Special Event").toString());
notify.setSpecialContent(jsonObj.get("splEvtDes")
.toString());
notifyData.add(notify);
} catch (JSONException e) {
// do nothing
return;
}
} else {
notify.setdetail(intent.getStringExtra("Detail"));
notify.setappUpdate(intent.getStringExtra("App Update"));
notify.setcontentUpdate(intent.getStringExtra("Content Update"));
notify.setSpecial(intent.getStringExtra("Special Event"));
notify.setSpecialContent(intent.getStringExtra("splEvtDes"));
notifyData.add(notify);
}
}
String message = null;
if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
createNotificationForAppUpdate();
} else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
message = notifyData.get(0).getdetail();
generateNotification(context, message);
} else {
System.out
.println("=-----------------------------inside else_________________________");
message = notifyData.get(0).getSpecialContent();
generateNotification(context, message);
}
}
最佳答案
根据您的示例代码,似乎您正在尝试在 Appupdate、contentUpdate 和特殊内容的情况下执行不同的操作。因此,根据您的代码创建三个方法。第一个用于 AppUpdate,第二个用于内容更新,最后一个用于特殊内容。
String message = null;
if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
createNotificationForAppUpdate();
//This will notify for App update
} else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
message = notifyData.get(0).getdetail();
//This will notify for Content updte
generateNotification(context, message);
} else {
//This will notify for special content
message = notifyData.get(0).getSpecialContent();
generateNotification(context, message);
}
AppUpdate方法
private void createNotificationForAppUpdate()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("Enter here playstore link"));
startActivity(browserIntent);
}
内容通知方法。
private void generateNotification(Context context,String message){
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(context, YourActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(context, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle("Title")
.setContentText(message)
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
}
也相应地创建特殊方法。 希望这会有所帮助,祝你好运
关于java - 如何在点击推送通知时打开特定 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27184879/