2017-02-15 1 views
0

Iam application de notification d'écriture. Pour définir une notification, j'utilise AlarmManager.Notification Huawei EMUI

Tout semble bien fonctionner, malheureusement pas chez Huawei. Lorsque l'utilisateur ferme les notifications de l'application qui ne parviennent pas (sur les autres appareils LG, NEXUS fonctionne bien).

Une idée de comment y remédier?

intent = new Intent(context, AlarmReceiver.class); 
    sender = PendingIntent.getBroadcast(context, alarmId, intent, 0); 


    am.set(android.app.AlarmManager.RTC_WAKEUP, timeToAlarm, sender); 

EDIT

public class AlarmReceiver extends BroadcastReceiver { 
private static final String TAG = "AlarmReceiver"; 
private PowerManager.WakeLock wakeLock; 

@Override 
public void onReceive(Context context, Intent intent) { 

    PowerManager pm = (PowerManager) context 
      .getSystemService(Context.POWER_SERVICE); 
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); 

    wakeLock.acquire(); 

    new NotificationUtils(context, null).postNotification(context.getString(R.string.notification_title), context.getString(R.string.notification_message)); 

    unlock(); 
} 

private void unlock() { 
    if (wakeLock != null) 
     if (wakeLock.isHeld()) 
      wakeLock.release(); 

    wakeLock = null; 
} 

}

+0

vous avez dit que vous fermez votre application. Ok mais il y a un service ou un récepteur de diffusion en arrière-plan? Si vous fermez l'application Android peut le tuer. –

+0

Je viens de le faire glisser à partir de la liste des processus. Est-il possible que seulement sur ce type de dispositifs, après il tue toutes les émissions? et si oui, que puis-je faire pour l'empêcher? – Ikazuchi

+0

vous ne pouvez pas compter sur votre application pour lancer une notification, car il peut être annulé à tout moment. Vous devez implémenter un broadcastreceiver ou un service en arrière-plan et lancer une notification à partir de celui-ci. Dans d'autres travaux de téléphone mais est aléatoire. –

Répondre

0

Il y a des applications Protected option dans les paramètres. Allez-y et sélectionnez votre application

+0

Je l'ai fait, ne fonctionne toujours pas: < – Ikazuchi

0

J'ai trouvé cette solution en testant mes notifications. Après avoir écrit cet appel ifHuaweiAlert() dans OnCreate() et voir ce qui se passe.

private void ifHuaweiAlert() { 
    final SharedPreferences settings = getSharedPreferences("ProtectedApps", MODE_PRIVATE); 
    final String saveIfSkip = "skipProtectedAppsMessage"; 
    boolean skipMessage = settings.getBoolean(saveIfSkip, false); 
    if (!skipMessage) { 
     final SharedPreferences.Editor editor = settings.edit(); 
     Intent intent = new Intent(); 
     intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"); 
     if (isCallable(intent)) { 
      final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(this); 
      dontShowAgain.setText("Do not show again"); 
      dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        editor.putBoolean(saveIfSkip, isChecked); 
        editor.apply(); 
       } 
      }); 

      new AlertDialog.Builder(this) 
        .setIcon(android.R.drawable.ic_dialog_alert) 
        .setTitle(R.string.huawei_protected_apps) 
        .setMessage(String.format("%s" + " "requiers to be enabled in Protected Apps) , getString(R.string.app_name))) 
        .setView(dontShowAgain) 
        .setPositiveButton("Protected Apps", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          huaweiProtectedApps(); 
         } 
        }) 
        .setNegativeButton(android.R.string.cancel, null) 
        .show(); 
     } else { 
      editor.putBoolean(saveIfSkip, true); 
      editor.apply(); 
     } 
    } 
} 

private boolean isCallable(Intent intent) { 
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
      PackageManager.MATCH_DEFAULT_ONLY); 
    return list.size() > 0; 
} 

private void huaweiProtectedApps() { 
    try { 
     String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity"; 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
      cmd += " --user " + getUserSerial(); 
     } 
     Runtime.getRuntime().exec(cmd); 
    } catch (IOException ignored) { 
    } 
} 

private String getUserSerial() { 
    //noinspection ResourceType 
    Object userManager = getSystemService("user"); 
    if (null == userManager) return ""; 

    try { 
     Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null); 
     Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null); 
     Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass()); 
     Long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle); 
     if (userSerial != null) { 
      return String.valueOf(userSerial); 
     } else { 
      return ""; 
     } 
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException ignored) { 
    } 
    return ""; 
}