Java 如何单击推送通知打开特定活动
我正在开发一个android应用程序,在该应用程序中,我使用GCM添加通知服务。问题是我想在单击通知时打开特定的活动。我不知道如何完成这个任务。我还想更改服务器端和客户端的更改。活动名称将由服务器提供,然后当我单击通知时,活动将打开。请帮帮我 我试过这个代码Java 如何单击推送通知打开特定活动,java,android,Java,Android,我正在开发一个android应用程序,在该应用程序中,我使用GCM添加通知服务。问题是我想在单击通知时打开特定的活动。我不知道如何完成这个任务。我还想更改服务器端和客户端的更改。活动名称将由服务器提供,然后当我单击通知时,活动将打开。请帮帮我 我试过这个代码 protected void onMessage(Context context, Intent intent) { Log.i(TAG, "Received message. Extras:
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);
}
}
protectedvoid-onMessage(上下文、意图){
Log.i(标签,
“收到消息。附加信息:************************************************************************************”
+intent.getExtras());
//String message=getString(R.String.gcm_message);
//显示消息(上下文、消息);
//通知用户
字符串不活动=intent.getAction();
ArrayList notifyData=新的ArrayList();
if(inAction.equals(“com.google.android.c2dm.intent.RECEIVE”)){
String json_info=intent.getExtras().getString(“数据”);
通知=新通知();
如果(json_info!=null){
试一试{
JSONObject jsonObj=新的JSONObject(json_信息);
notify.setdetail(jsonObj.get(“Detail”).toString());
notify.setappUpdate(jsonObj.get(“App Update”).toString());
notify.setcontentUpdate(jsonObj.get(“内容更新”)
.toString());
notify.setSpecial(jsonObj.get(“特殊事件”).toString());
notify.setSpecialContent(jsonObj.get(“splEvtDes”)
.toString());
notifyData.add(notify);
}捕获(JSONException e){
//无所事事
返回;
}
}否则{
notify.setdetail(intent.getStringExtra(“Detail”);
notify.setappUpdate(intent.getStringExtra(“应用程序更新”);
notify.setcontentUpdate(intent.getStringExtra(“内容更新”);
通知.setSpecial(intent.getStringExtra(“特殊事件”));
notify.setSpecialContent(intent.getStringExtra(“splEvtDes”);
notifyData.add(notify);
}
}
字符串消息=null;
if(notifyData.get(0.getappUpdate().equalsIgnoreCase(“是”)){
createNotificationForAppUpdate();
}else if(notifyData.get(0.getcontentUpdate().equalsIgnoreCase(“是”)){
message=notifyData.get(0.getdetail();
生成化(上下文、消息);
}否则{
系统输出
.println(“=-----------------------------内部其他内容”;
message=notifyData.get(0.getSpecialContent();
生成化(上下文、消息);
}
}
您可以指定任何活动作为推送通知的接收者:
<intent-filter>
<action android:name="PACKAGE_NAME.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
您可以指定任何活动作为推送通知的接收者:
<intent-filter>
<action android:name="PACKAGE_NAME.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
提到您在挂起的意图中指定了活动(此处为MainActivity.class),如下所示
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
提到您在挂起的意图中指定了活动(这里是MainActivity.class),如下所示
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
根据您的示例代码,您似乎试图在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());
}
因此也为特殊的创造方法。
希望这将有助于好运根据您的示例代码,似乎您正在尝试对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);
}
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
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());
}
因此也为特殊的创造方法。
希望这将有助于好运
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
我遇到了类似的问题,在我加入“android.intent.action.VIEW”之后,活动就会打开。
我遇到了类似的问题,在我加入“android.intent.action.VIEW”之后,活动就会打开。将此代码放在何处。对不起,我是android开发的新手。看看这个例子或者展示你的代码,我会告诉你过去在哪里。把代码放在哪里。对不起,我是android开发的新手。看看这个例子,或者展示你的代码,我会告诉你过去在哪里。