当使用谷歌云消息发送消息到我的Android应用程序时我无法弄清楚如何打开是或否的对话框(如javasrcript确认框),如果他们点击是或什么都不做,在浏览器中打开一个网站如果他们没有。
我花了太多时间而且讨厌向你展示这个基本代码而没有应该在最后的失败代码,但我只是出于想法并尝试了太多我在网上发现的样本变体。我怀疑他们失败了,因为我使用了错误的上下文,或者因为我试图从这个服务类中做到这一点。
public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onMessage( Context myContext, Intent intent ) {
// TODO Auto-generated method stub
Log.i( LOG_TAG, "GCMIntentService onMessage called" );
Log.i( LOG_TAG, "Message is: " + intent.getStringExtra( "data" ) );
JSONObject o = API.getJSONObj(intent.getStringExtra( "data" ));
String URL = "";
String message = "";
try {
URL = o.getString("URL");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
message = o.getString("message");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Code to open dialog or website goes here */
答案 0 :(得分:9)
onMessage方法。要打开通知对话框,您可以在onMessage中启动另一个活动,并将您的代码(用于对话框)放在该Activity.Or中,您也可以进行通知。像这样: -
public class GCMIntentService extends GCMBaseIntentService
{
@Override
protected void onMessage( Context myContext, Intent intent)
{
<your code>
//if you want to show any dialog directly.
Intent i = new Intent(myContext,<your Activity.class>);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.putExtra("message", message);
myContext.startActivity(i);
//if you want to show message through notification.
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon,arg1.getStringExtra("message"), when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context,<Your Activity>.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, title, arg1.getStringExtra("message"), intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
}
请告诉我这是否有助于你和圣诞节结婚:-)。
答案 1 :(得分:0)
尝试此工作,在GCMIntentService中编写生成通知中的上述代码
private static void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.appicon,
message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context,
YourClassName.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
当点击通知时,它会转到您的应用程序中的活动,之后在活动中您可以创建对话框以及您想要放入活动中的任何内容