How to Save SharedPreferences Parsing Alerts

I am sending push notifications from pasrse.com, I want to save push notifications so that they can be seen later after closing the application.

I can receive notifications in a separate text form, but if I switch to another activity, notifications will disappear.

MainActivity.java

public class MainActivity extends ActionBarActivity {
Button Notify;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Notify = (Button)findViewById(R.id.notify);


        Notify.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(MainActivity.this, DisplayPush.class);
            intent.putExtra("com.parse.Data","String text");
                startActivity(intent);    

        }
    });
    }

DisplayPush.java

public class DisplayPush extends Activity {
String jsonData;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Titletobeput = "title";
    public static final String Messagetobeput = "mess";

    SharedPreferences sharedpreferences;
@Override
public void onBackPressed(){
   Intent myIntent = new Intent(DisplayPush.this,MainActivity.class);
   startActivity(myIntent);
  //  overridePendingTransition(R.anim.from_middle, R.anim.to_middle);
    finish();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);

    TextView notification_title = (TextView) findViewById(R.id.title);
    TextView notification_message = (TextView) findViewById(R.id.message);


    ParseAnalytics.trackAppOpened(getIntent());

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if(extras !=null)
      if(extras.containsKey("com.parse.Data"))
         jsonData = extras.getString("com.parse.Data");


    //Bundle extras = intent.getExtras();
    String jsonData = extras.getString("com.parse.Data");

    try{
        JSONObject notification = new JSONObject(jsonData);
        String Title = notification.getString("title");
        String Message = notification.getString("alert");


        notification_message.setText(Message);
        notification_title.setText(Title);

        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);



        SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putString(Titletobeput, Title);
        editor.putString(Messagetobeput, Message);
        editor.commit();
    }
    catch(JSONException e){
        Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
    }


}
}

Notification.xml (notifications are shown)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"

        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp" />

    <TextView
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="80dp" />

</RelativeLayout>

ActivityMain.xml (just a button to display the notification button)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hasan.parsereceiveandshow.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press the button to display notifications" />

    <Button
        android:id="@+id/notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="63dp"
        android:text="See Notifications" />

</RelativeLayout>

Is there a way by which I can automatically display a full notification in the status / notification dialog box? Because long messages are truncated.

+4
source share
3 answers

, Parse Library (Parse 1.9.3). ,

i.e PushNotificationBroadcastReceiver, ParsePushBroadcastReceiver onReceive :

    public class PushNotificationBroadcastReceiver extends ParsePushBroadcastReceiver {

    /**
     * Everytime a Push notification message comes, It is received here.
     *
     * @param context
     * @param intent  contains pushed Json or Message
     */
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();
        try {
            JSONObject notification;
            notification = new JSONObject(extras.getString(ParsePushBroadcastReceiver.KEY_PUSH_DATA));
//  Save Json to Shared Preferences. Add your Own implementation here
            String title = notification.getString("title");
            String message = notification.getString("alert");

            SharedPreferences sharedpreferences = context.getSharedPreferences("myPreferences", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("title", title);
            editor.putString("message", message);
            editor.commit();
//----------------------------------------------------------------------------------

        } catch (JSONException e) {
            Log.e("Error Parsing JSON", e.getMessage());
        }

    }

    /**
     * Returns the Activity Class which you want to open when notification is tapped
     *
     * @param context
     * @param intent
     * @return Activity Class which you want to open
     */
    @Override
    protected Class<? extends Activity> getActivity(Context context, Intent intent) {
        return SomeActivity.class;
    }

    /**
     * This method is called once user taps on a Push Notification
     *
     * @param context
     * @param intent
     */
    @Override
    protected void onPushOpen(Context context, Intent intent) {
        super.onPushOpen(context, intent);
    }

}

PushNotificationBroadcastReceiver ,

        <receiver
            android:name="com.package.PushNotificationBroadcastReceiver"
            android:exported="false">

            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>

. ,

, :)

+3

SharedPreference:

SharedPreferences prefs = getSharedPreferences(<Name>, <Mode>);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(<KEY>, notification_title.getText().toString()).commit();
//Similarly, save other infos

:

SharedPreferences prefs = getSharedPreferences(<Name>, <Mode>);
String title = prefs.getString(<KEY>, <DefaultValue>); 
+1

" push-, ".

I did this and it works perfectly the way I want.

DisplayPush.java

public class DisplayPush extends Activity {
    String jsonData;
    Button save;
    TextView notification_title;
    TextView notification_message;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Titletobeput = "title";
    public static final String Messagetobeput = "alert";

    SharedPreferences sharedpreferences;
    @Override
    public void onBackPressed(){
        Intent myIntent = new Intent(DisplayPush.this,MainActivity.class);
        startActivity(myIntent);
        //  overridePendingTransition(R.anim.from_middle, R.anim.to_middle);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
        save = (Button) findViewById(R.id.button);
        TextView notification_title = (TextView) findViewById(R.id.title);
         TextView notification_message = (TextView) findViewById(R.id.message);


        ParseAnalytics.trackAppOpened(getIntent());

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null)
            if (extras.containsKey("com.parse.Data"))
                jsonData = extras.getString("com.parse.Data");


        //Bundle extras = intent.getExtras();
        String jsonData = extras.getString("com.parse.Data");

        try {
            JSONObject notification = new JSONObject(jsonData);
            String Title = notification.getString("title");
            String Message = notification.getString("alert");


            notification_message.setText(Message);
            notification_title.setText(Title);


            sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

            String n = notification_title.getText().toString();
            String e = notification_message.getText().toString();
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString(Titletobeput, n);
            editor.putString(Messagetobeput, e);
            editor.commit();

        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
        }
    }

    public void Get(View view) {
        notification_title = (TextView) findViewById(R.id.title);
        notification_message = (TextView) findViewById(R.id.message);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Titletobeput)) {
            notification_title.setText(sharedpreferences.getString(Titletobeput, ""));
        }
        if (sharedpreferences.contains(Messagetobeput)) {
            notification_message.setText(sharedpreferences.getString(Messagetobeput, ""));

        }
    }
}

From notification.xmljust call android:onClick="Get"the "Save" button.

Here is my working draft with this response code.  https://www.dropbox.com/s/atvi2gee5bycq6k/ParseReceiveAndShow-master1.zip?dl=0

Moderators, before deleting my answer, please review the code / project.

+1
source

All Articles