我想将解析推送通知从 parse.com 收到的消息显示为弹出消息。
目前,当我从托盘中选择通知时,它只会打开应用程序。我希望能够在弹出窗口中显示该消息。
答案 0 :(得分:0)
写一个广播接收器来处理推送,这里是接收器..
public class MyCustomReceiver extends BroadcastReceiver {
String title, time, msg;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data")
: "";
Log.e("message ", " " + message);
JSONObject jObject;
try {
if (message != null && !message.equals("")) {
jObject = new JSONObject(message);
String msg=jObject.getString("msg");
Intent pupInt = new Intent(context, ShowPopUp.class);
Intent.FLAG_ACTIVITY_NEW_TASK );
context.getApplicationContext().startActivity(pupInt);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
将此添加到您的清单
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="your package name.MyCustomReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="your package name.UPDATE_STATUS" />
</intent-filter>
</receiver>
你的弹出消息类......
public class ShowPopUp extends Activity implements OnClickListener {
Button ok;
Button cancel;
boolean click = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Message");
setContentView(R.layout.popupdialog);
ok = (Button)findViewById(R.id.popOkB);
ok.setOnClickListener(this);
cancel = (Button)findViewById(R.id.popCancelB);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
}
使用此代码发送推送通知
push.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
JSONObject obj;
try {
obj = new JSONObject();
obj.put("alert", "tiltle of your push");
obj.put("action", "your package name.UPDATE_STATUS");
obj.put("msg", "push test");
ParsePush push = new ParsePush();
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("deviceType", "android");
push.setQuery(query);
push.setData(obj);
push.sendInBackground();
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "Message Sent", 10000).show();
}
});
希望你的问题能够得到解决......