Getting Parse Push Notification in List view

2019-08-14 03:47发布

问题:

I am using parse push notification for my app , the notification comes fine but on clicking the notification I need to show them in a list view , I searched for tutorials but I could not find any . Please help me . Please explain with code , I am new to android . Thanks in advance. here is my custom receiver.

      public class CustomReciever extends BroadcastReceiver {

      NotificationCompat.Builder mBuilder;
      Intent resultIntent;
  int mNotificationId = 001;
   Uri notifySound;
   String alert;

@Override
public void onReceive(Context context, Intent intent) {
    try{
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
        alert = json.getString("alert").toString();
    }catch (JSONException e){

    }
    mBuilder = new  NotificationCompat.Builder(context);
    mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(alert));
    mBuilder.setPriority(Notification.PRIORITY_HIGH);
    mBuilder.setSmallIcon(R.drawable.syncytium);
    mBuilder.setContentText(alert);
    mBuilder.setContentTitle("Syncytium");
    mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    mBuilder.setAutoCancel(true);

    resultIntent = new Intent(context, com.omc.sunny.syncytium.syncytium.Notification.class);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager notificationManager =
            (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

    notificationManager.notify(mNotificationId,mBuilder.build());
}
  }

here is my Notification class

   public class Notification extends AppCompatActivity {
    TextView notifTv;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);
    notifTv = (TextView)findViewById(R.id.notif);

}

@Override
protected void onNewIntent(Intent intent) {
    String message = getIntent().getExtras().getString("alert");
    notifTv.setText(message);
}

 }

回答1:

At first add this to your manifest

-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="YOURPACKAJE.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="YOURPACKAJE.permission.C2D_MESSAGE" />

add it to application tag

    <service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="YOURPACKAJE.notifications.MyReceiver"
        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>
    <receiver
        android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
            <category android:name="YOURPACKAJE" />
        </intent-filter>
    </receiver>

then create class in packaje YOURPACKAJE.notifications.MyReceiver

import java.util.List;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.TextView;

import com.parse.ParseAnalytics;
import com.parse.ParsePushBroadcastReceiver;


public class MyReceiver extends ParsePushBroadcastReceiver {

    protected void onPushReceive(Context mContext, Intent intent) {

        Log.e("ParsePush", "RECIVED");
        if (intent.hasExtra("com.parse.Data")){
            String jsonString=intent.getExtras().getString("com.parse.Data");

            Log.e("", "json " + jsonString);

                JSONObject json = new JSONObject(jsonString);

                String title= json.getString("title");
                String message= json.getString("message");
    // then call your method to create manually your custom notification with pending intent
//in intent putExtra("title",title), putExtra("message",message)
//and the after opening in Activity catch this intent
                 }
             }
        }

in wersite parse.com send notification like JSON

{"title":"your tittle is here","message":"your message"}


回答2:

dont use getIntent() , use intent in method:

@Override
    protected void onNewIntent(Intent intent) {
        String message = intent.getExtras().getString("alert");
        notifTv.setText(message);
    }