如何显示每个48小时后,Android通知? [英] How to show android notification after every 48hours?

查看:188
本文介绍了如何显示每个48小时后,Android通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过以下code,对于它是工作的罚款Android通知,但它给我通知弹出,当我开始我的Andr​​oid应用程序。


  

我想告诉每一个48小时后通知,我该怎么办呢?


什么样的​​变化,我需要做为了这个工作?

通知code

 意图notificationIntent =新意图(MainActivity.this,Activity.class);
        的PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,0,notificationIntent,0);         NotificationManager notificationManager =(NotificationManager)MainActivity.this
                    .getSystemService(Context.NOTIFICATION_SERVICE);         通知的NotI =新NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setTicker(股票信息)                            .setWhen(System.currentTimeMillis的())
                            .setContentTitle(HELLO)
                            .setContentText(请检查我们更新报)
                            .setContentIntent(co​​ntentIntent)
                            //最多三个动作按钮可以添加
                            .setAutoCancel(真).build();
        INT notifyID = 0;
        notificationManager.notify(notifyID,NotI位);


解决方案

下面是一些接近您需要在您的主要活动内容:

 意图notificationIntent =新意图(背景下,ShowNotification.class);
    的PendingIntent contentIntent = PendingIntent.getService(上下文,0,notificationIntent,
                                                           PendingIntent.FLAG_CANCEL_CURRENT);    AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    am.cancel(contentIntent);
    am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()
            + AlarmManager.INTERVAL_DAY * 2,AlarmManager.INTERVAL_DAY * 2,contentIntent);

然后,在名为ShowNotification.java另一个文件,添加如下(假设你的主要活动被命名为MainActivity):

 进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.os.IBinder;
进口android.support.v4.app.NotificationCompat;
进口android.util.Log;公共类ShowNotification延伸服务{    私人最终静态字符串变量=ShowNotification;    @覆盖
    公共无效的onCreate(){
        super.onCreate();        意图mainIntent =新意图(这一点,MainActivity.class);        notificationManager notificationManager
            =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);        通知的NotI =新NotificationCompat.Builder(本)
            .setAutoCancel(真)
            .setContentIntent(PendingIntent.getActivity(这一点,0,mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle(HELLO+ System.currentTimeMillis的())
            .setContentText(请检查我们更新报)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker(股票信息)
            .setWhen(System.currentTimeMillis的())
            。建立();        notificationManager.notify(0,NotI位);        Log.i(TAG,通知创造);
    }    @覆盖
    公众的IBinder onBind(意向意图){
        // TODO自动生成方法存根
        返回null;
    }
}

I tried following code for android notification which is working fine but it is giving me notification popup when I start my android app.

I want to show an notification after every 48hours, how can I do it?

What changes do I need to make in order for this to work?

Notification code

 Intent notificationIntent = new Intent(MainActivity.this, Activity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

         NotificationManager notificationManager = (NotificationManager) MainActivity.this
                    .getSystemService(Context.NOTIFICATION_SERVICE);

         Notification noti = new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setTicker("ticker message")

                            .setWhen(System.currentTimeMillis())
                            .setContentTitle("HELLO")
                            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
                            .setContentIntent(contentIntent)
                            //At most three action buttons can be added
                            .setAutoCancel(true).build();
        int notifyID =0;
        notificationManager.notify(notifyID, noti);

解决方案

Here's something close to what you need in your main activity:

    Intent notificationIntent = new Intent(context, ShowNotification.class);
    PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent,
                                                           PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.cancel(contentIntent);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
            + AlarmManager.INTERVAL_DAY * 2, AlarmManager.INTERVAL_DAY * 2, contentIntent);

Then, in another file named ShowNotification.java, add the following (assuming your main activity is named MainActivity):

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class ShowNotification extends Service {

    private final static String TAG = "ShowNotification";

    @Override
    public void onCreate() {
        super.onCreate();

        Intent mainIntent = new Intent(this, MainActivity.class);

        NotificationManager notificationManager
            = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification noti = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("HELLO " + System.currentTimeMillis())
            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("ticker message")
            .setWhen(System.currentTimeMillis())
            .build();

        notificationManager.notify(0, noti);

        Log.i(TAG, "Notification created");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

这篇关于如何显示每个48小时后,Android通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆