2016-11-24 13 views
3

ロック画面にカスタム通知が表示されています。私は通知がクリックされた後、私のアプリケーションにメッセージを送信するために、保留中の放送を使用しています。これは後で放送受信機での活動を開始する。カスタム通知でロック画面のロックを解除するように促すメッセージが表示される

問題は、通知をクリックすると、ロック画面から消え、ロック画面の後ろにアクティビティが開始されるという問題です。ユーザーに画面のロックを解除するよう要求するものではありません。

私の要件は、ブロードキャストが通知のクリックイベントによって送信されるとすぐに画面のロックを解除するようにユーザーに要求することです。それ、どうやったら出来るの?

私の問題のように見えるthis質問が見つかりました。しかし、残念ながら答えはありません。

私が行った通知の作成について説明するコードは次のとおりです。

/** 
* Method to render Notification 
*/ 
private void showNotification() { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    /* set mandatory stuff on builder */ 

    Notification notification = builder.build(); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
     notification.bigContentView = createCustomView(context); 
    } 

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(getNotificationId(), notification); 
} 

/** 
* Method to return notification click's PendingIntent 
*/ 
private PendingIntent getNotificationClickIntent() { 
    Intent bcIntent = new Intent(OPEN_APP); 
    bcIntent.putExtra(EXTRA_DEEP_LINK_URL, getDeepLinkUrl()); 

    return PendingIntent.getBroadcast(
     context, getReqCode(), bcIntent, PendingIntent.FLAG_ONE_SHOT); 
} 

/** 
* Method to create custom view for notification 
*/ 
private RemoteViews createCustomView(Context context) { 
    RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.custom_layout); 

    if (customView != null) { 
     // set dat on views 

     customView.setOnClickPendingIntent(R.id.my_view, getNotificationClickIntent()); 
    } 

    return customView; 
} 

答えて

0

利用活動

PendingIntent.getActivity(context, 0, intent, 
           PendingIntent.FLAG_UPDATE_CURRENT); 

代わりにサービスまたはブロードキャスト

の保留目的で意図更新:

私の場合は、サービスを使用します。

private fun activityPendingIntent(context: Context, action: String? = null): PendingIntent { 
    Timber.d("activityPendingIntent") 
    val intent = Intent(context, DummyBackgroundActivity::class.java) 
    action?.let { intent.action = action } 
    return PendingIntent.getActivity(context, ACTIVITY_NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT) 
} 

DummyBackgroundActivity

class DummyBackgroundActivity : AppCompatActivity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     val service = Intent(this, BackgroundService::class.java) 
     service.action = intent.action 
     startService(service) 
    } 

    override fun onResume() { 
     super.onResume() 
     finish() 
    } 
} 

サービス:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 
    Timber.d("onStartCommand intent = %s", intent?.action) 
    when (intent?.action) { 
     //Handle it 
    } 

    return Service.START_NOT_STICKY 
} 

私はあなたが同じ使用してブロードキャストを複製願っています。

+0

いいえこれは私の場合は使用できません。私のコードはライブラリの一部です。図書館では、どのアクティビティーを開始する必要があるか分かりません。だから私は今放送に依存している。受信側では、このライブラリを使用するクライアントアプリケーションがアクティビティ起動コードを実装します。 –

+0

@RahulRaveendranあなたのケースでは、ダミーのアクティビティを作成し、onCreateでイベントをブロードキャストし、onResumeでアクティビティを終了することができます。私は答えを更新しました。それが役に立てば幸い。 –

+0

アップデート担当者に感謝します!私はこの考えを念頭に置いていました。これに対処する他の方法があるかどうかを見たいと思った。その間に活動を紹介しているCozは現時点で実現不可能です:( –

関連する問題