我想使用解析接收推送通知并将其显示在活动中。如何从解析通知中获取值?

时间:2015-06-06 09:08:10

标签: android parse-platform

我试过这个来保存推送

ParseQuery parseq=ParseInstallation.getQuery();
parseq.whereEqualTo("role", "manager");

ParseObject p=ParseInstallation.getCurrentInstallation();

ParsePush push = new ParsePush();
push.setQuery(parseq);
push.setMessage("My measage");
push.sendInBackground();

现在我希望在收到通知后收到我的消息并在活动中打印回复。

2 个答案:

答案 0 :(得分:0)

您需要执行以下操作才能从推送通知中获取值。

  1. 在主包中添加ParsePushReciever。

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.support.v4.app.NotificationCompat;
    
    
    import com.parse.ParsePushBroadcastReceiver;
    
    
    public class ParsePushReciever extends ParsePushBroadcastReceiver {
    
    
    @Override
    public void onPushOpen(Context context, Intent intent) {
        AppLog.e("Push", "Clicked");
        Intent i = new Intent(context, MainActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Push Notification",intent.getExtras().get(ParsePushBroadcastReceiver.KEY_PUSH_DATA).toString());
        NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    
        builder.setSmallIcon(R.drawable.launcher);
    
        Intent newIntent=new Intent(context,MainActivity.class);
        newIntent.putExtra(context.getString(R.string.navigation_from_notification),true);
        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pi=PendingIntent.getActivity(context, 0, newIntent, 0);
    
        builder.setContentIntent(pi);
    
        builder.setContentText("Push Notification");
    
        Log.d("Notification", strMsg);
    
        nm.notify(1, builder.build());
    }
    }
    
  2. 在清单文件中添加接收器:

    <receiver android:name=".ParsePushReciever" 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>
    <meta-data
        android:name="com.parse.push.notification_icon"
        android:resource="@drawable/launcher" />
    
  3. 检查MainActivity中的通知:

    if(getIntent()!=null) {
    
        if (getIntent().getExtras()!=null &&
            getIntent().getExtras().getBoolean(getString(R.string.navigation_from_notification))) {
    
           //get data from intent and display it in your activity.
        }
    }
    

答案 1 :(得分:0)

这个小代码可以通过推送获得所有文本,你可以用它来做你的事情:

{{1}}

将它放在ParsePushBroadcastReceiver的onReceive()方法中。

我希望这对你有所帮助。