您好,如果您在 jsonObject 中不使用“title”和“alert”键,那么它不会在通知栏中创建默认通知。我可以制作自定义通知而不是默认通知。
您可以看到我的代码并从警报对话框代码中更改自定义通知代码。看看:
发送通知的代码:
String otherChannelName = "channel" + user_id;
// send push notfication
String str_objid = PreferenceSetting.getObjId(mActivity);
String str_uname = PreferenceSetting.getUName(mActivity);
ParsePush push = new ParsePush();
push.setChannel(otherChannelName);
push.setMessage(message);
JSONObject data = null;
try {
data = new JSONObject("{\"action\": \""
+ NChatActivity.BUDDY_CHAT_PUSH_ACTION + "\"" + ",\"from\": \""
+ str_objid + "\"" + ",\"to\": \"" + user_id
+ "\"" + ",\"a\" : \"" + message + "\""
+ ",\"t\" : \"" + type + "\"" + "}");
} catch (JSONException e) {
e.printStackTrace();
}
push.setData(data);
push.sendInBackground();
接收通知的代码:
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data")
: "";
String channel = extras.getString("com.parse.Channel");
JSONObject jObject = null;
try {
if (message != null && !message.equals("")) {
jObject = new JSONObject(message);
from = jObject.getString("from");
messageText = jObject.getString("a");
type = jObject.getString("t");
}
}
catch (JSONException e) {
e.printStackTrace();
}
ActivityManager am =(ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase("com.pappchat"))
{
if (type.equals("Papp App")) {
if(taskInfo.get(0).topActivity.getClassName().equals("com.pappchat.NChatActivity"))
{
LoadDataTwo.getfriendInfo(from,context);
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", messageText);
b.putString("from", from);
msg.setData(b);
if(handler.sendMessage(msg)){
Log.d(TAG, "send message successfully");
}else{
Log.d(TAG, "unable to send message.");
}
}
else
{
notify(context,intent,jObject);
}
}
这里我在名为 notify(context,intent,jObject) 的方法中制作了自定义通知,您可以在此方法中制作警报对话框(当应用程序处于后台时)。
看看这个链接,可能对你有用:automatically-open-an-activity-without-user-action for notification