我正在开发一个Android应用程序,其中我正在使用GCM添加通知服务。问题是我想在点击通知时打开特定活动。我不知道如何执行此任务。我还想更改服务器端和客户端的更改。活动名称由服务器提供,然后当我单击通知时,活动将打开。请帮帮我。
我试过这段代码。
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);
}
}
答案 0 :(得分:2)
根据您的示例代码,似乎您正在尝试在Appupdate,contentUpdate和Special内容的情况下执行不同的操作。因此,根据您的代码创建三个方法。第一个用于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());
}
因此也为特殊创造方法。 希望这有助于GOOD LUCK
答案 1 :(得分:1)
您可以指定任何活动作为推送通知的接收者:
<intent-filter>
<action android:name="PACKAGE_NAME.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
此活动的意图过滤器指定响应推送通知将启动哪个活动(PACKAGE_NAME是您的Android应用包)
因此,您可以在活动中添加此意图过滤器,然后单击“推送通知”即可打开该活动。
Referenec:More Info
答案 2 :(得分:1)
在Pending Intent中提及您的具体活动(此处为MainActivity.class)
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
答案 3 :(得分:0)
private void notificationMethod(){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(yourActivity.this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("title")
.setContentText("text");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(yourActivity.this, wantToOpenActivity.this);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(yourActivity.this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack( wantToOpenActivity.this);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
int mId=001;;
mNotificationManager.notify(mId, mBuilder.build());
}
答案 4 :(得分:0)
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
我遇到了类似的问题,在我加入&#34; android.intent.action.VIEW&#34;后,活动就开始了。