0

I am trying to make a simple notification in my android app , but it always cracks. What I'm doing is that I have a button in the main activity when clicked it moves the following notification class : 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());
}

}

2 Answers 2

2

Main issue is with the 'addParentStack' call, Your trying to add the Notification class as parent. It should be your OfferNotification Activity which is the parent.

  sb.addParentStack(OfferNotification.class);

Other issue is the Intent action should be an Activity.

  Intent resultIntent = new Intent(this , YourActivity.class);

Next issue is with the setSmallIcon , you should pass a valid resource id.

  .setSmallIcon(R.drawable.your_icon)

Make all these three changes, Your are out of cracks.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I made all these three changes , and no cracks anymore, but still no notifications are created.
check the device settings,if show notification is enabled for your app
the notification is enabled , I think it's because I'm on the application
0

Pretty sure when your app "cracks" you're throwing a RemoteServiceException and your logcat reads something like:

Bad notification posted from package: Couldn't create icon:

This happens because you're passing Activity.RESULT_OK in NotificationCompat.Builder.setSmallIcon. You need to be passing in a Drawable resource.

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

1 Comment

Thanks , that works , it's not cracking , but no notifications are formed :\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.