2015-07-13 77 views
5

主旋律后,即使单击通知后,删除的Android棒棒糖通知是发送通知,使用PushNotification可在店内的新的提议使用谷歌云消息甚至无法使用我的应用程序的AUTOCANCEL

private void sendNotification(Context context, String message) { 

SharedPreferences mPrefs = getSharedPreferences("MYAPP", Context.MODE_PRIVATE); 

int uniqueNumber = this.getSharedPreferences("MYAPP", Context.MODE_PRIVATE).getInt("uniqueNumber", 0); 
    Log.d("GCM: GN", "Message: " + message); 

    uniqueNumber = ++uniqueNumber; 
    int icon = R.drawable.ic_launcher; 

    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 intent = new Intent(GCMIntentService.this,OpenActivity.class); 
    intent.putExtra(OpenActivity.EXTRA_KEY, "navigate"); 
    intent.putExtra(OpenActivity.GET_NOTIFY_ID_EXTRA, uniqueNumber); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
    notification.setLatestEventInfo(context, title, message, pendingIntent); 
    notification.flags |= Notification.DEFAULT_LIGHTS|Notification.FLAG_AUTO_CANCEL ; 

    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(uniqueNumber, notification); 

    SharedPreferences.Editor prefsEditor = mPrefs.edit(); 
    prefsEditor.putInt("uniqueNumber", uniqueNumber); 
    prefsEditor.commit(); 

} 
用户

伙计们,这段代码不适用于棒棒糖。

任何帮助赞赏...

+0

是否有原因,您没有使用支持库的[NotificationCompat.Builder](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html)甚至[Notification .Builder](http://developer.android.com/reference/android/app/Notification.Builder.html)?您使用的代码已过时4年。 – ianhanniballake

+0

没有特别的原因。我以前使用过Notification.Builder,但我得到了同样的问题。 – saikiran

+0

与我的应用程序中的NotificationCompat.Builder相同的问题。 – saikiran

回答

0

试试这个。它对我来说工作正常,就像我已经实施到我的项目一样。

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

如果工作不正常,请使用下面的代码清除您的意图任务。

Intent notificationIntent; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); 
+0

感谢您的贡献,但它不工作... – saikiran

+0

嘿,因为你说意图旗帜,它的工作正常。 –

+0

嘿,即使使用该标志后,该问题也没有解决...... – saikiran

0

请添加此代码并将其添加到您的代码中。

NOTIFICATION_COUNT = 10;

private void generateNotification(final String message, final String title, final String userType, final String userId) { 

     Log.e("message:", message); 
     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent notificationIntent; 

     notificationIntent = new Intent(context, GroupMessagesActivity.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     Notification notification = new Notification(icon, message, when); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
     notification.flags |= Notification.DEFAULT_LIGHTS; 
     notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL; 

     Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
     long[] pattern = { 0, 100, 1500 }; 
     v.vibrate(pattern, -1); 
     notificationManager.notify(NOTIFICATION_COUNT, notification); 
     NOTIFICATION_COUNT = 1 + NOTIFICATION_COUNT; 
    } 
+0

只需全局定义NOTOFICATION_COUNT = 10即可。 –

+0

这也不起作用 – tiru

+0

此代码也不能正常工作.. – saikiran

0
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

请试试这个。

+0

我也使用过这些通知标志也.. 我有问题的待定意图... – saikiran

+0

这工作正常在我的联系。 –

0

请尝试更改您的活动主题。如果您使用自己的自定义主题,请尝试将其替换为由android提供的主题。

相关问题