如何在Activity处于前台时抑制Android中的Parse通知?

时间:2015-02-26 09:51:47

标签: android parse-platform

我正在使用Parse for Android Platform进行通知。我已成功收到推送通知。

我希望在应用程序运行时停止此通知的回调方法

截至目前我已尝试查找,但我不知道是否有任何回调方法会在通知到来时调用活动。请帮帮我!

1 个答案:

答案 0 :(得分:0)

在Manifest文件中,将其添加到Application Tag中。

<service android:name="com.parse.PushService" />

    <receiver
        android:name="yourPackageName.receiver.ParsePushBroadcast"
        android:exported="false" >
        <intent-filter>
            <action android:name="yourPackageName.UPDATE_STATUS" />
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.OPEN" />
            <action android:name="com.parse.push.intent.DELETE" />
        </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" />

            <!--
              IMPORTANT: If you change the package name of this sample app,
              change "com.parse.tutorials.pushnotifications" in the lines
              below to match the new package name.
            -->
            <category android:name="yourPackageName" />
        </intent-filter>
    </receiver>

在清单文件中的应用程序标记上方添加以下行。

<permission
    android:name="yourPackageName.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="yourPackageName.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

创建一个广播接收器ParsePushBroadcast之后。

import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.RingtoneManager;
import android.util.Log;

import com.parse.ParsePushBroadcastReceiver;

    public class ParsePushBroadcast extends ParsePushBroadcastReceiver {
    private static final String TAG = "MyBroadcastReceiver";

    @Override
    public void onReceive(final Context context, Intent intent) {

    try {
        String action = intent.getAction();
        @SuppressWarnings("unused")
        String channel = intent.getExtras().getString("com.parse.Channel");
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
        String title = "New alert!";
        if (json.has("alert"))
            title = json.getString("alert");

        if (MainActivity.isActivityOpen == true) {
            if (MainActivity.ACTION.equals(action))
                displayAlert(context, title);
        } else {
            generateNotification(context, R.drawable.notification_icon,     title);
            }
        } catch (Exception e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        }

    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        super.onPushReceive(context, intent);
    }

@SuppressWarnings("deprecation")
public static void generateNotification(Context context, int icon, String message) {
    // Show the notification
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MainActivity.class);

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.vibrate = new long[] { 500, 500 };
    notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

    notificationManager.notify(0, notification);
}

    private void displayAlert(Context context, String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder((MainActivity) context);
    builder.setTitle("Title");
    builder.setMessage(title).setCancelable(false).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    if (!alert.isShowing())
        alert.show();
}
}

isActivityOpen 是我在Main活动中创建的静态变量。此变量的目的是活动是否开放。

重要提示:如果清单中有此标记,请在清单中删除此标记。

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

在onResume方法的Activity类中添加。

ParsePushBroadcast parsePushBroadcast = new ParsePushBroadcast();
IntentFilter filter = new IntentFilter(ACTION);
this.registerReceiver(parsePushBroadcast, filter);

在Activity

中添加此常量
public static final String ACTION = "com.parse.push.intent.RECEIVE";