mas*_*ary 3 android google-cloud-messaging
当使用谷歌云消息发送消息到我的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 */
Run Code Online (Sandbox Code Playgroud)
每次收到通知时都会调用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);
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我这是否有助于你和圣诞节结婚:-).
| 归档时间: |
|
| 查看次数: |
4140 次 |
| 最近记录: |