2015-06-19 7 views
5

मेरे एप्लिकेशन में मेरे पास GCM के साथ एकीकृत पुश अधिसूचना है। जब भी अधिसूचना दिखाई दे रही है तो यह ठीक काम कर रहा है। लेकिन पुश अधिसूचना तब भी आती है जब उपयोगकर्ता ऐप के अंदर होता है। मैं अधिसूचनाएं तब दिखाना चाहता हूं जब उपयोगकर्ता ऐप के बाहर हो।केवल ऐप नहीं चल रहा है जब ऐप एंड्रॉइड

यहाँ पुश अधिसूचना के लिए अपने कोड है:

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

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

कोई सुझाव में अत्यधिक सराहना की जाएगी !!!

+0

आप के लिए अनुमति जोड़ लिया है <का उपयोग करता है-अनुमति एंड्रॉइड: name = "com.google.android.c2dm.permission.RECEIVE" /> <अनुमति एंड्रॉइड: नाम = ".... अनुमति। सी 2 डी_एमईएसएसएजी" एंड्रॉइड: सुरक्षा लेवल = "हस्ताक्षर" /> <उपयोग- अनुमति एंड्रॉइड: नाम = "..... अनुमति.C2D_MESSAGE" /> ? – Amit

+0

आपके पास नियंत्रण है कि क्या दिखाना है, या नोटिफिकेशन में दिखाना न दिखाएं। मुझे लगता है कि आपको लगता है कि ऐप अग्रभूमि में है। एसओ के इस लिंक के बारे में चर्चा करता है। बस इसे देखें। http://stackoverflow.com/questions/5504632/how-can-i-tell-if-android-app-is-running-in-the-foreground –

+0

@ प्रवेश करें मैंने मैनिफेस्ट फ़ाइल में सभी अनुमतियां जोड़ दी हैं। उसके बाद केवल – John

उत्तर

2

जाँच करने के लिए विधि नीचे बुला और मूल्य लौटने सही या गलत के आधार पर से कि आपने एप्लिकेशन या नहीं अग्रभूमि में कुछ इस तरह करते हैं काम के अपने बाकी

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

मुझे एक और संदेह है कि आप साफ़ कर सकते हैं ?? – John

+0

पैकेज के लिए नाम मैं विशेष वर्ग नाम दे सकता हूं, – John

+0

हाँ आप क्लास नाम का भी उपयोग कर सकते हैं प्रारूप भी होगा: 'pacakgename + classname' – Pankaj

संबंधित मुद्दे