Display Alert when push arrives

泪湿孤枕 提交于 2020-01-02 05:46:06

问题


How Can I Display an alert when push notification arrives from C2DM or GCM. Right now I am displaying notifications on status bar. So How can I get a notification as an alert.


回答1:


Popping a dialog in the middle of something a user is doing is a wildly user hostile thing to do. What if they're playing Angry Birds and you've just ruined their shot? Only the platform can and should get away with that.

Use the notification area, like Google intended, UNLESS you app currently has control (your activity is in running state). Then just use that activity's context to pop an AlertDialog(). You can detect if the activity is running by overriding onResume() and onPause() - anything that happens in between is your activity's time.

An AlertDialog can be popped like this:

new AlertDialog.Builder(Ctxt) //Use an activity object here
    .setMessage(R.string.MyMessageID) //Provide a message here... A string or a string ID will do
    .setCancelable(true) //If you want them to be able to dismiss with a Back button
    .setNegativeButton(R.string.IDS_NO, null) //No action on NO, right?
    .setPositiveButton(R.string.IDS_YES, OnYesClickListener) //Plug your own listener...
    .create()
    .show();

For a simple message/Yes/No dialog, an AlertDialog would suffice. For more complicated UI, derive a class from Dialog and design your own layout.




回答2:


For Alert you need to write code in

generateNotification(Context context, String message)

in this method.

Thanks



来源:https://stackoverflow.com/questions/12803557/display-alert-when-push-arrives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!