我想在我的Android应用程序中做一个简单的通知,但它总是裂缝。 我正在做的是,当点击它时,我在main activity有一个按钮,它会移动以下通知类:package 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());
}

}

===============>>#1 票数: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)

做出所有这三个变化,你已经没有了。

===============>>#2 票数:0

非常确定当你的应用程序“破解”你正在抛出RemoteServiceException并且你的logcat读取如下内容:

从包中发布错误通知:无法创建图标:

发生这种情况是因为您在NotificationCompat.Builder.setSmallIcon传递了Activity.RESULT_OK 您需要传入Drawable资源。

NotificationCompat.Builder mBuilder = 
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.your_notification_icon)
            .setContentTitle("Entangle:You got a new offer!")
            .setContentText(notification);

  ask by user3100088 translate from so

未解决问题?本站智能推荐: