在列表视图中获取解析推送通知

时间:2015-09-01 17:48:32

标签: android parse-platform push-notification

我正在为我的应用程序使用解析推送通知,通知正常但是点击我需要在列表视图中显示它们的通知,我搜索了教程但我找不到任何。请帮我 。请用代码解释,我是android新手。提前致谢。 这是我的定制接收器。

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

这是我的通知类

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

 }

2 个答案:

答案 0 :(得分:1)

首先将此添加到您的清单中  

-->
<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" />

将其添加到应用程序代码

    <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>

然后在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
                 }
             }
        }

在wersite parse.com中发送通知,如JSON

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

答案 1 :(得分:0)

请勿使用getIntent(),请在方法中使用intent

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