2014-11-21 64 views
0

我刚刚在我的应用中实施了Parse推送通知。我希望能够显示推送通知,但我不希望应用程序在用户按下推送通知时打开。相反,我只是想通知被解雇。停止显示活动 - 推送通知(解析)

我会想象它会被ParsePushBroadcastReciever处理,但是我找不到任何在线哪个适合我的目的。

这里是我的子类ParsepushBroadcastReciever:

public class Receiver extends ParsePushBroadcastReceiver { 

     @Override 
     public void onPushOpen(Context context, Intent intent) { 
      Log.e("Push", "Clicked"); 
      Intent i = new Intent(context, HomeScreen.class); 
      i.putExtras(intent.getExtras()); 
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(i); 
     } 
    } 

回答

2

为了让您以编程方式关闭通知,您将需要使用NotificationManager实例传递这是对NotificationManager传递到notify()的ID叫cancel()(自这实际上是将通知推送到通知窗格中)。您无法单独使用Parse SDK来执行此操作,因为您需要自行控制通知群体。

首先,你需要安装一个NotificationManager实例,然后当通知已准备好要推你分配给它,你可以在以后引用取消这样当值:您可以通过采取控制看

public class MyParsePushBroadcastReceiver extends ParsePushBroadcastReceiver { 

    NotificationManager mNotificationManager; 
    int notification_id = 0; 

    @Override 
    public void onPushOpen(Context context, Intent intent) { 
    Log.e("Push", "Clicked"); 
    mNotificationManager.cancel(notification_id)    
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     mNotificationManager = (NotificationManager)context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
     super.onReceive(context, intent); 
     ... 
    } 

    @Override 
    protected Notification getNotification(Context context, Intent intent) { 
     Notification n = super.getNotification(context, intent); 
     notification_id = intent.getExtras().getInt("NOTIFICATION_TYPE"); 
     mNotificationManager.notify(notification_id, n); 
     return null; 
    } 
} 

所以NotificationManager(而不是将它传递给Parse SDK以分配一些未知值),我们可以确切知道在调用cancel时要使用哪些值。我让系统构建我的Notification对象(我使用super.getNotification()得到它),但您也可以自己使用Notification builder自行创建通知。