点击本地通知时打开android应用

时间:2020-10-13 15:22:59

标签: android android-notifications

我使用此代码创建本地通知,并在30秒后使用AlarmManager将其显示。

  NotificationCompat.Builder builder = new NotificationCompat.Builder( context, default_notification_channel_id ) ;
    String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
    builder.setContentTitle( "Check new words for "+currentDate ) ;
    builder.setContentText("30 seconds delay") ;
    builder.setDefaults(Notification.DEFAULT_SOUND);
    builder.setSmallIcon(R.mipmap.ic_launcher_round ) ;
    builder.setAutoCancel( true ) ;
    builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;

    Intent notificationIntent = new Intent( context, AlarmReceiver. class ) ;
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra(AlarmReceiver. NOTIFICATION_ID , 1 ) ;
    notificationIntent.putExtra(AlarmReceiver. NOTIFICATION , builder.build()) ;
    PendingIntent pendingIntent1 = PendingIntent. getBroadcast ( context, 6977 , notificationIntent , PendingIntent. FLAG_UPDATE_CURRENT ) ;
    long futureInMillis = SystemClock. elapsedRealtime () + 30000 ;

    assert alarmManager != null;


    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP , futureInMillis , 1000*10,pendingIntent1);

这是AlarmReceiver中的代码

import static com.example.couponnectcom.MainActivity.NOTIFICATION_CHANNEL_ID;
public class AlarmReceiver extends BroadcastReceiver {
    public static String NOTIFICATION_ID = "hello" ;
    public static String NOTIFICATION = "aha" ;
    @Override
    public void onReceive(Context context, Intent intent) {

        // For our recurring task, we'll just display a message
    
  
        ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
        ActivityManager.getMyMemoryState(myProcess);
        Boolean isInBackground;
        isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
        if(isInBackground) {
            Toast.makeText(context.getApplicationContext(), "12. APPLICATION BACKGROUND RUN NOTIFICATION", Toast.LENGTH_LONG).show();
            NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
            Notification notification = intent.getParcelableExtra( NOTIFICATION ) ;
            if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
                int importance = NotificationManager. IMPORTANCE_HIGH ;
                NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
                assert notificationManager != null;
                notificationManager.createNotificationChannel(notificationChannel) ;
            }
            int id = intent.getIntExtra( NOTIFICATION_ID , 0 ) ;
            assert notificationManager != null;
            notificationManager.notify(id , notification) ;
        }
        

    }
}

当用户点击通知时,我希望打开我的Android应用...这怎么可能?

1 个答案:

答案 0 :(得分:0)

您需要使用PendingIntent。有关更多信息,请参见documentation

有关如何从具有PendingIntent的通知中启动活动的明确说明,请参见this article.

PendingIntent openActivityIntent = 
    PendingIntent.getActivity(context, 
                              0, 
                              new Intent(context, YourActivity.class), 
                              0);
builder.setContentIntent(openActivityIntent);