2014-04-20 42 views
0

我想在我的android应用程序中做一个简单的通知,但它总是会出现裂缝。 我在做什么是我在main activity点击时有一个按钮,它会移动以下通知类: package com.megasoft.entangle;Android简单通知

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()); 
} 

} 

回答

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) 

使所有这三个变化,你没有裂缝。

+0

谢谢我做了所有这三个变化,并没有裂缝了,但仍然没有创建通知。 – user3100088

+1

检查设备设置,如果显示通知已启用您的应用程序 – Libin

+0

通知已启用,我认为这是因为我在应用程序 – user3100088

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); 
+0

谢谢,这是有效的,它不会破解,但没有通知形成:\ – user3100088