2017-11-18 31 views
1

我在BroadcastReceiver()中遇到了Notification問題。 據我所知,我的代碼之前工作正常,但現在不起作用。 有時出現NotificationTicker,但沒有出現標題和內容。 這是我的代碼。我的搜索無法幫助我找到問題所在。BroadCastReceiver中的通知不起作用

這裏是我的代碼:

private void MyNotification(Context context) { 

    String NotificqationText = "NotificqationText"; 
    String NotificationTitle = "NotificationTitle "; 
    String NotificationTicker = "NotificationTicker"; 
    PendingIntent MyPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Splash.class), 0); 


    NotificationCompat.Builder MyNB = new NotificationCompat.Builder(context); 
    MyNB.setSmallIcon(R.drawable.icon); 
    MyNB.setContentTitle(NotificationTitle); 
    MyNB.setContentText(NotificqationText); 
    MyNB.setTicker(NotificationTicker); 
    MyNB.setAutoCancel(true); 
    MyNB.setContentIntent(MyPendingIntent); 

    Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); 
    MyNB.setLargeIcon(MyPicture); 

    NotificationCompat.BigPictureStyle MyPicStyle = new NotificationCompat.BigPictureStyle().bigPicture(MyPicture); 
    MyPicStyle.setSummaryText("Etude can makes our life Enlightened"); 
    MyNB.setStyle(MyPicStyle); 


    MyNB.setStyle(new NotificationCompat.BigTextStyle()); 
    NotificationCompat.BigTextStyle MyText = new NotificationCompat.BigTextStyle(); 
    MyText.bigText(NotificqationText); 
    MyText.setBigContentTitle(NotificationTitle); 

    NotificationManager MyNotifyManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    MyNotifyManager.notify(1, MyNB.build()); 

} 

我用吐司消息找到我的BroadcastReceiver的作品或沒有找到廣播正常工作,並只通知有問題

+0

當我再次檢查我的代碼是真實的,但一些像「清潔大師」的應用程序已被阻止我的手機內我的通知 –

回答

0

試試這個代碼:

private void MyNotification(Context context) { 

    String NotificqationText = "NotificqationText"; 
    String NotificationTitle = "NotificationTitle "; 
    String NotificationTicker = "NotificationTicker"; 
    Intent intent = new Intent(this, Splash.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |(Intent.FLAG_ACTIVITY_SINGLE_TOP 
      | Intent.FLAG_ACTIVITY_CLEAR_TASK)); 
    PendingIntent MyPendingIntent = PendingIntent.getActivity(this, 0, 
      intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 


    Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); 

    Notification MyNB = new Notification.Builder(this) 
    .setSmallIcon(R.drawable.icon) 
    .setLargeIcon(MyPicture) 
    .setStyle() 
    .setBigContentTitle(NotificationTitle) 
    .setContentTitle(NotificationTitle) 
    .setContentText(NotificqationText) 
    .setTicker(NotificationTicker) 
    .setAutoCancel(true) 
    .setContentIntent(MyPendingIntent) 
    .build(); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    MyNB.flags |= Notification.FLAG_SHOW_LIGHTS; 
    MyNB.flags |= Notification.FLAG_AUTO_CANCEL; 
    MyNB.defaults = Notification.DEFAULT_ALL; 

    notificationManager.notify((int)System.currentTimeMillis(), MyNB); 
    } 
+0

thx兄弟,因爲我再次檢查我的代碼是真實的b如「清潔大師」一些應用程序已被阻止我的手機內的通知。 –