2015-06-19 19 views
5

Trong ứng dụng của tôi, tôi có thông báo đẩy được tích hợp với GCM. Nó hoạt động tốt bất cứ khi nào thông báo xuất hiện. Nhưng thông báo đẩy đến ngay cả khi người dùng ở trong ứng dụng. Tôi muốn thông báo chỉ xuất hiện khi người dùng ở bên ngoài ứng dụng.Chỉ hiển thị thông báo đẩy khi ứng dụng không chạy android

Đây là mã của tôi để thông báo push:

GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    ComponentName comp = new ComponentName(GCMNotificationIntentService.class.getPackage().getName(), 
      GCMNotificationIntentService.class.getName()); 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
} 
} 

GCMnotificationIntentService.Class

public class GCMNotificationIntentService extends IntentService { 

public static int notificationId = 10; 
private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 
private boolean login_status = false; 


public GCMNotificationIntentService() { 
    super("GcmIntentService"); 
} 

public static final String TAG = GCMNotificationIntentService.class.getSimpleName(); 

@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    // The getMessageType() intent parameter must be the intent you received 
    // in your BroadcastReceiver. 
    String messageType = gcm.getMessageType(intent); 

    if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
     /* 
     * Filter messages based on message type. Since it is likely that 
     * GCM will be extended in the future with new message types, just 
     * ignore any message types you're not interested in, or that you 
     * don't recognize. 
     */ 
     if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
      sendNotification("Send error: " + extras.toString()); 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
      sendNotification("Deleted messages on server: " + extras.toString()); 
      // If it's a regular GCM message, do some work. 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
      sendNotification(extras.getString("message")); 
      Log.i(TAG, "Received: " + extras.toString()); 
     } 
    } 
    // Release the wake lock provided by the WakefulBroadcastReceiver. 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 

private void sendNotification(String msg) { 

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle(getString(R.string.notification_title)) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg); 
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    mBuilder.setSound(uri); 
    mBuilder.setContentIntent(contentIntent); 
    mBuilder.setAutoCancel(true); 
    mNotificationManager.notify(notificationId++, mBuilder.build()); 
} 
} 

Trong ManifestFile

<receiver 
     android:name=".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="com.medlife.deliveryagent" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".GCMNotificationIntentService" /> 

gợi ý Bất kỳ sẽ được đánh giá cao !!!

+0

Bạn đã thêm phép ? – Amit

+0

Bạn có quyền kiểm soát nội dung hiển thị hoặc hiển thị không hiển thị trong thông báo. Chỉ cần bạn phải tìm ứng dụng là ở phía trước tôi nghĩ. Liên kết này của SO thảo luận về điều đó. Chỉ cần kiểm tra xem nó ra. http://stackoverflow.com/questions/5504632/how-can-i-tell-if-android-app-is-running-in-the-foreground –

+0

@Amit Tôi đã thêm tất cả quyền trong tệp kê khai. sau đó chỉ hoạt động – John

Trả lời

2

Do something như thế này để kiểm tra xem bạn ứng dụng là trong foreground hay không bằng cách gọi dưới phương pháp và tùy thuộc vào trở về giá trị true hoặc false làm phần còn lại làm việc của bạn

public boolean checkApp(){ 
     ActivityManager am = (ActivityManager) this 
       .getSystemService(ACTIVITY_SERVICE); 

     // get the info from the currently running task 
     List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 

     ComponentName componentInfo = taskInfo.get(0).topActivity; 
     if (componentInfo.getPackageName().equalsIgnoreCase("YourPackage")) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
+0

Tôi có một nghi ngờ nữa mà bạn có thể xóa? – John

+0

Đối với packageName tôi có thể cung cấp cho tên lớp cụ thể, – John

+0

Vâng, bạn có thể sử dụng tên lớp cũng định dạng sẽ là: 'pacakgename + classname' – Pankaj

Các vấn đề liên quan