Cocos2dx:应用程序关闭时推送通知

时间:2014-07-24 06:36:54

标签: android ios cocos2d-x

我们可以实现我在cocos2dx中告诉的标题。 我的意思是,当使用不开放应用程序7天(例子)。 我们将推送通知,我们可以通过Cocos实现它跨平台吗? 请帮忙。

2 个答案:

答案 0 :(得分:4)

它不能跨平台。因为cocos2d-x没有处理,但没有人阻止你自己为不同的操作系统实现它,

您可以使用宏定义CC_PLATFORM_TARGET来编写目标代码。

实现iOS之类的 how to create local notifications in iphone app

实现Android之类的 Local Notifications in Android?

如果您在编写obj-c / c ++混合代码时需要帮助, How can I use C++ with Objective-C in XCode

或JNI桥 Android Cocos2dX JNI Bridge

答案 1 :(得分:2)

我不明白你到底想要什么,但我想这样做可以使用这样的AlarmManager:

Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, heure);   //choose here for a week
        cal.add(Calendar.MINUTE, minute);

        Intent intent = new Intent(this, MyBroadcastReceiver.class);

        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

并使用BroadcastReceiver进行通知,如下所示:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent resultIntent = new Intent(context,
            YourActivity.class);   //to open when click the notification

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // Because clicking the notification opens a new ("special") activity,
    // there's no need to create an artificial back stack.
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Title")
    .setContentText("Message");

    mBuilder.setAutoCancel(true);   //TODO enlever pour le mettre avec l'intent
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) context
            .getSystemService(Application.NOTIFICATION_SERVICE);


    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

} }