我想在我的Android应用程序中做一个简单的通知,但它总是破裂。
我正在做的是,main activity
点击时有一个按钮,它会移动以下通知类:
包com.megasoft.entangle;
import java.util.concurrent.ExecutionException;
import org.json.JSONException;
import org.json.JSONObject;
import com.megasoft.requests.GetRequest;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.widget.TextView;
public class OfferNotification extends Activity {
String notification = "";
String offerer = "";
String description = "";
String request = "";
String amount = "";
String notificationID = "1";
final String link = "Click here to view";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_notification);
GetRequest getRequest = new GetRequest("http://entangle2.apiary.io/notification/1");
getRequest.addHeader("sessionID" , "testest");
getRequest.execute("http://entangle2.apiary.io/notification/1");
try {
JSONObject json = new JSONObject(getRequest.get());
description = json.getString("description");
offerer = json.getString("offerer");
request = json.getString("request");
amount = json.getString("Amount");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notification = offerer + " has offered " + amount + " on your " + request + link;
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(notification);
createNotification();
}
public void createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(RESULT_OK)
.setContentTitle("Entangle:You got a new offer!")
.setContentText(notification);
Intent resultIntent = new Intent(this , Notification.class);
TaskStackBuilder sb = TaskStackBuilder.create(this);
sb.addParentStack(Notification.class);
sb.addNextIntent(resultIntent);
PendingIntent pIntent =
sb.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pIntent);
NotificationManager notificationMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int ID = Integer.parseInt(notificationID);
notificationMgr.notify(ID , mBuilder.build());
}
}
答案 0 :(得分:2)
主要问题是“addParentStack”调用,您尝试将Notification类添加为父级。它应该是你的OfferNotification Activity
父母。
sb.addParentStack(OfferNotification.class);
其他问题是Intent操作应该是Activity。
Intent resultIntent = new Intent(this , YourActivity.class);
下一个问题是setSmallIcon
,您应该传递有效的资源ID。
.setSmallIcon(R.drawable.your_icon)
进行所有这三项改动,你的确无法解决。
答案 1 :(得分:0)
非常确定你的应用程序"裂缝"你扔了一个RemoteServiceException
,你的logcat就是这样的:
从包裹发布错误通知:无法创建图标:
这是因为您在Activity.RESULT_OK
中传递了NotificationCompat.Builder.setSmallIcon
。您需要传递Drawable
资源。
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.your_notification_icon)
.setContentTitle("Entangle:You got a new offer!")
.setContentText(notification);