当前位置: 首页 > 知识库问答 >
问题:

当应用程序关闭时发送通知

颜思淼
2023-03-14

当应用程序完全关闭时,如何以编程方式发送通知?

示例:用户关闭应用程序,也在Android Taskmanager中,然后等待。应用程序应在X秒后或应用程序检查更新时发送通知。

我试图使用这些代码示例,但是:

  • 应用程序关闭时推送通知-活动太多/无法工作
  • 我如何让我的应用程序在关闭时发送通知很多信息,但我不知道如何处理
  • 当android应用程序关闭时,如何发送本地通知?-很多信息,但我不知道如何处理

如果可以,试着用一个例子来解释它,因为初学者(像我一样)可以通过这种方式更容易地学习它。

共有3个答案

齐弘业
2023-03-14

您可以使用服务检查活动应用程序,并在活动未运行时显示通知。

姜宏放
2023-03-14

您可以使用报警管理器执行此操作。请遵循以下步骤:

1)使用alarmManager创建X秒后的警报。

Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("NotificationText", "some text");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ledgerId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 'X seconds in milliseconds', pendingIntent);

2) 在应用程序中使用AlarmBroadCast接收器。

在清单文件中声明:

<receiver android:name=".utils.AlarmReceiver">
    <intent-filter>
        <action android:name="android.media.action.DISPLAY_NOTIFICATION" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

3) 在广播接收器的on receive中,您可以创建通知。

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // create notification here
    }
}
袁宜民
2023-03-14

您可以使用此服务您所需要做的就是在活动生命周期中启动此服务onStop()。使用以下代码:start Service(new Intent(this,NotificationService.class));然后您可以创建一个新的JavaClass并将代码粘贴到其中:

public class NotificationService extends Service {

    Timer timer;
    TimerTask timerTask;
    String TAG = "Timers";
    int Your_X_SECS = 5;


    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);

        startTimer();

        return START_STICKY;
    }


    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");


    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        stoptimertask();
        super.onDestroy();


    }

    //we are going to use a handler to be able to run in our TimerTask
    final Handler handler = new Handler();


    public void startTimer() {
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
        timer.schedule(timerTask, 5000, Your_X_SECS * 1000); //
        //timer.schedule(timerTask, 5000,1000); //
    }

    public void stoptimertask() {
        //stop the timer, if it's not already null
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void initializeTimerTask() {

        timerTask = new TimerTask() {
            public void run() {

                //use a handler to run a toast that shows the current timestamp
                handler.post(new Runnable() {
                    public void run() {

                        //TODO CALL NOTIFICATION FUNC
                        YOURNOTIFICATIONFUNCTION();

                    }
                });
            }
        };
    }
}

在此之后,您只需要将服务与清单结合起来。xml:

<service
            android:name=".NotificationService"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="your.app.domain.NotificationService" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
 类似资料:
  • 我想知道是否有可能在应用程序打开然后关闭时向设备发送本地通知。 当我的应用程序打开并在后台时,它已经工作了。 谢啦 编辑:我想我还不够清楚: 我想在给定时间发送本地通知,即使应用程序当时未运行。

  • 我想用Xamarin格式的C#创建一个基于文本的Android游戏。 在故事中,我想设置角色任务,这需要一些时间,例如“我去挖这个洞,完成后给你打电话。” 如何将通知设置为在设置的时间之后显示?例如,上述声明可能需要10分钟,然后用户收到继续游戏的通知? 我一周前才开始做C#,所以如果这是noobish,或者已经被问到了,我道歉。我到处都找过,但有几种类型的通知,当我试图理解它时,似乎我在读法语。

  • 我目前正在开发Android(Cordova)应用程序,我正在使用Oneignal推送通知。首先,如果我关闭应用程序,推送通知不会被传递,所以我不得不添加一个Cordova插件来保持我的应用程序在后台运行: 我在deviceready之后的代码: 谢了。

  • 如果应用程序关闭,我将如何使其发送通知?我正在制作一个从web服务器读取数据的应用程序,只有当该应用程序关闭且web服务器上的值更改时,我才需要向用户发送通知。

  • 这里是模块链接https://github.com/zo0r/react-native-push-notification在ios通知工作正常时,在后台和前台也,但在android前台工作正常,但在后台我得到了通知,当点击该通知应用程序重新启动,并没有显示任何警报在android, 在iOS中,这些功能运行良好。但在Android系统中,我面临着这个问题 任何人能给我一些建议,如何解决它,任何帮助